2017-10-27 17:54:45 +08:00
|
|
|
if (Rails.env.production? && SiteSetting.logging_provider == 'lograge') || ENV["ENABLE_LOGRAGE"]
|
2017-10-27 22:54:50 +08:00
|
|
|
require 'lograge'
|
|
|
|
|
2017-11-15 09:02:34 +08:00
|
|
|
if Rails.configuration.multisite
|
|
|
|
Rails.logger.formatter = ActiveSupport::Logger::SimpleFormatter.new
|
|
|
|
end
|
|
|
|
|
2017-10-27 17:54:45 +08:00
|
|
|
Rails.application.configure do
|
|
|
|
config.lograge.enabled = true
|
|
|
|
|
2017-12-08 08:30:48 +08:00
|
|
|
config.lograge.custom_payload do |controller|
|
|
|
|
begin
|
2017-12-12 17:22:38 +08:00
|
|
|
username =
|
|
|
|
begin
|
|
|
|
controller.current_user&.username
|
|
|
|
rescue Discourse::InvalidAccess
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
ip =
|
|
|
|
begin
|
|
|
|
controller.request.remote_ip
|
|
|
|
rescue ActionDispatch::RemoteIp::IpSpoofAttackError
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2017-12-08 08:30:48 +08:00
|
|
|
{
|
2017-12-12 17:22:38 +08:00
|
|
|
ip: ip,
|
|
|
|
username: username,
|
2017-12-08 08:30:48 +08:00
|
|
|
}
|
|
|
|
rescue => e
|
|
|
|
Rails.logger.warn("Failed to append custom payload: #{e.message}\n#{e.backtrace.join("\n")}")
|
|
|
|
{}
|
|
|
|
end
|
|
|
|
end
|
2017-12-05 11:51:03 +08:00
|
|
|
|
2017-10-27 17:54:45 +08:00
|
|
|
config.lograge.custom_options = lambda do |event|
|
2017-12-08 08:30:48 +08:00
|
|
|
begin
|
|
|
|
exceptions = %w(controller action format id)
|
2017-10-27 22:54:50 +08:00
|
|
|
|
2017-12-08 08:30:48 +08:00
|
|
|
params = event.payload[:params].except(*exceptions)
|
|
|
|
params[:files].map!(&:headers) if params[:files]
|
2017-11-01 08:37:11 +08:00
|
|
|
|
2017-12-08 08:30:48 +08:00
|
|
|
output = {
|
|
|
|
params: params.to_query,
|
|
|
|
database: RailsMultisite::ConnectionManagement.current_db,
|
|
|
|
}
|
2017-11-02 14:40:18 +08:00
|
|
|
|
2017-12-08 08:30:48 +08:00
|
|
|
if data = Thread.current[:_method_profiler]
|
|
|
|
sql = data[:sql]
|
2017-11-28 14:00:13 +08:00
|
|
|
|
2017-12-08 08:30:48 +08:00
|
|
|
if sql
|
|
|
|
output[:db] = sql[:duration] * 1000
|
|
|
|
output[:db_calls] = sql[:calls]
|
|
|
|
end
|
2017-11-28 14:00:13 +08:00
|
|
|
|
2017-12-08 08:30:48 +08:00
|
|
|
redis = data[:redis]
|
2017-11-28 14:00:13 +08:00
|
|
|
|
2017-12-08 08:30:48 +08:00
|
|
|
if redis
|
|
|
|
output[:redis] = redis[:duration] * 1000
|
|
|
|
output[:redis_calls] = redis[:calls]
|
|
|
|
end
|
2017-11-28 14:00:13 +08:00
|
|
|
end
|
2017-11-25 08:10:49 +08:00
|
|
|
|
2017-12-08 08:30:48 +08:00
|
|
|
output
|
2017-12-11 14:52:48 +08:00
|
|
|
rescue RateLimiter::LimitExceeded
|
|
|
|
# no idea who this is, but they are limited
|
|
|
|
{}
|
2017-12-08 08:30:48 +08:00
|
|
|
rescue => e
|
|
|
|
Rails.logger.warn("Failed to append custom options: #{e.message}\n#{e.backtrace.join("\n")}")
|
|
|
|
{}
|
|
|
|
end
|
2017-10-27 17:54:45 +08:00
|
|
|
end
|
2017-11-01 08:37:11 +08:00
|
|
|
|
2017-11-14 12:50:26 +08:00
|
|
|
if ENV["LOGSTASH_URI"]
|
2017-11-01 08:37:11 +08:00
|
|
|
config.lograge.formatter = Lograge::Formatters::Logstash.new
|
2017-11-14 12:50:26 +08:00
|
|
|
|
|
|
|
require 'discourse_logstash_logger'
|
|
|
|
|
|
|
|
config.lograge.logger = DiscourseLogstashLogger.logger(
|
|
|
|
uri: ENV['LOGSTASH_URI'], type: :rails
|
|
|
|
)
|
2017-11-01 08:37:11 +08:00
|
|
|
end
|
2017-10-27 17:54:45 +08:00
|
|
|
end
|
|
|
|
end
|