2019-05-02 17:17:27 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-06-13 00:56:58 -05:00
|
|
|
if (Rails.env.production? && SiteSetting.logging_provider == 'lograge') || (ENV["ENABLE_LOGRAGE"] == "1")
|
2017-10-27 09:54:50 -05:00
|
|
|
require 'lograge'
|
|
|
|
|
2017-11-14 19:02:34 -06:00
|
|
|
if Rails.configuration.multisite
|
|
|
|
Rails.logger.formatter = ActiveSupport::Logger::SimpleFormatter.new
|
|
|
|
end
|
|
|
|
|
2017-10-27 04:54:45 -05:00
|
|
|
Rails.application.configure do
|
|
|
|
config.lograge.enabled = true
|
|
|
|
|
2018-02-20 22:19:59 -06:00
|
|
|
Lograge.ignore(lambda do |event|
|
|
|
|
# this is our hijack magic status,
|
|
|
|
# no point logging this cause we log again
|
|
|
|
# direct from hijack
|
|
|
|
event.payload[:status] == 418
|
|
|
|
end)
|
|
|
|
|
2017-12-07 18:30:48 -06:00
|
|
|
config.lograge.custom_payload do |controller|
|
|
|
|
begin
|
2017-12-12 03:22:38 -06:00
|
|
|
username =
|
|
|
|
begin
|
2018-08-19 21:58:56 -05:00
|
|
|
if controller.respond_to?(:current_user)
|
|
|
|
controller.current_user&.username
|
|
|
|
end
|
2020-06-24 21:25:28 -05:00
|
|
|
rescue Discourse::InvalidAccess, Discourse::ReadOnly, ActiveRecord::ReadOnlyError
|
2017-12-12 03:22:38 -06:00
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
ip =
|
|
|
|
begin
|
|
|
|
controller.request.remote_ip
|
|
|
|
rescue ActionDispatch::RemoteIp::IpSpoofAttackError
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2017-12-07 18:30:48 -06:00
|
|
|
{
|
2017-12-12 03:22:38 -06:00
|
|
|
ip: ip,
|
2018-04-16 23:07:13 -05:00
|
|
|
username: username
|
2017-12-07 18:30:48 -06:00
|
|
|
}
|
|
|
|
rescue => e
|
|
|
|
Rails.logger.warn("Failed to append custom payload: #{e.message}\n#{e.backtrace.join("\n")}")
|
|
|
|
{}
|
|
|
|
end
|
|
|
|
end
|
2017-12-04 21:51:03 -06:00
|
|
|
|
2017-10-27 04:54:45 -05:00
|
|
|
config.lograge.custom_options = lambda do |event|
|
2017-12-07 18:30:48 -06:00
|
|
|
begin
|
|
|
|
exceptions = %w(controller action format id)
|
2017-10-27 09:54:50 -05:00
|
|
|
|
2017-12-07 18:30:48 -06:00
|
|
|
params = event.payload[:params].except(*exceptions)
|
2018-07-18 19:23:59 -05:00
|
|
|
|
|
|
|
if (file = params[:file]) && file.respond_to?(:headers)
|
2018-07-19 18:57:00 -05:00
|
|
|
params[:file] = file.headers
|
2018-07-18 19:23:59 -05:00
|
|
|
end
|
|
|
|
|
2018-09-03 20:22:54 -05:00
|
|
|
if (files = params[:files]) && files.respond_to?(:map)
|
2018-09-03 21:16:21 -05:00
|
|
|
params[:files] = files.map do |f|
|
|
|
|
f.respond_to?(:headers) ? f.headers : f
|
2018-07-18 19:23:59 -05:00
|
|
|
end
|
|
|
|
end
|
2017-10-31 19:37:11 -05:00
|
|
|
|
2017-12-07 18:30:48 -06:00
|
|
|
output = {
|
|
|
|
params: params.to_query,
|
|
|
|
database: RailsMultisite::ConnectionManagement.current_db,
|
|
|
|
}
|
2017-11-02 01:40:18 -05:00
|
|
|
|
2018-02-20 22:19:59 -06:00
|
|
|
if data = (Thread.current[:_method_profiler] || event.payload[:timings])
|
2017-12-07 18:30:48 -06:00
|
|
|
sql = data[:sql]
|
2017-11-28 00:00:13 -06:00
|
|
|
|
2017-12-07 18:30:48 -06:00
|
|
|
if sql
|
|
|
|
output[:db] = sql[:duration] * 1000
|
|
|
|
output[:db_calls] = sql[:calls]
|
|
|
|
end
|
2017-11-28 00:00:13 -06:00
|
|
|
|
2017-12-07 18:30:48 -06:00
|
|
|
redis = data[:redis]
|
2017-11-28 00:00:13 -06:00
|
|
|
|
2017-12-07 18:30:48 -06:00
|
|
|
if redis
|
|
|
|
output[:redis] = redis[:duration] * 1000
|
|
|
|
output[:redis_calls] = redis[:calls]
|
|
|
|
end
|
2018-02-20 22:19:59 -06:00
|
|
|
|
|
|
|
net = data[:net]
|
|
|
|
|
|
|
|
if net
|
|
|
|
output[:net] = net[:duration] * 1000
|
|
|
|
output[:net_calls] = net[:calls]
|
|
|
|
end
|
2017-11-28 00:00:13 -06:00
|
|
|
end
|
2017-11-24 18:10:49 -06:00
|
|
|
|
2017-12-07 18:30:48 -06:00
|
|
|
output
|
2017-12-11 00:52:48 -06:00
|
|
|
rescue RateLimiter::LimitExceeded
|
|
|
|
# no idea who this is, but they are limited
|
|
|
|
{}
|
2017-12-07 18:30:48 -06:00
|
|
|
rescue => e
|
|
|
|
Rails.logger.warn("Failed to append custom options: #{e.message}\n#{e.backtrace.join("\n")}")
|
|
|
|
{}
|
|
|
|
end
|
2017-10-27 04:54:45 -05:00
|
|
|
end
|
2017-10-31 19:37:11 -05:00
|
|
|
|
2017-11-13 22:50:26 -06:00
|
|
|
if ENV["LOGSTASH_URI"]
|
2017-10-31 19:37:11 -05:00
|
|
|
config.lograge.formatter = Lograge::Formatters::Logstash.new
|
2017-11-13 22:50:26 -06:00
|
|
|
|
|
|
|
require 'discourse_logstash_logger'
|
|
|
|
|
|
|
|
config.lograge.logger = DiscourseLogstashLogger.logger(
|
|
|
|
uri: ENV['LOGSTASH_URI'], type: :rails
|
|
|
|
)
|
2018-04-12 23:08:27 -05:00
|
|
|
|
|
|
|
# Remove ActiveSupport::Logger from the chain and replace with Lograge's
|
|
|
|
# logger
|
2018-08-13 20:57:09 -05:00
|
|
|
Rails.logger.chained.pop
|
2018-04-12 23:08:27 -05:00
|
|
|
Rails.logger.chain(config.lograge.logger)
|
2017-10-31 19:37:11 -05:00
|
|
|
end
|
2017-10-27 04:54:45 -05:00
|
|
|
end
|
|
|
|
end
|