Rescue from errors when trying to append custom payloads to lograge output.

This commit is contained in:
Guo Xiang Tan 2017-12-08 08:30:48 +08:00
parent cff9b8846d
commit 2ff3444568
1 changed files with 36 additions and 31 deletions

View File

@ -8,46 +8,51 @@ if (Rails.env.production? && SiteSetting.logging_provider == 'lograge') || ENV["
Rails.application.configure do Rails.application.configure do
config.lograge.enabled = true config.lograge.enabled = true
# Seeing RuntimeError (Missing rack.input) config.lograge.custom_payload do |controller|
# for: `/topics/timings` begin
# {
# ip: controller.request.remote_ip,
# config.lograge.custom_payload do |controller| username: controller.current_user&.username,
# username = controller.try(:current_user)&.username rescue nil }
# { rescue => e
# ip: controller.request.remote_ip, Rails.logger.warn("Failed to append custom payload: #{e.message}\n#{e.backtrace.join("\n")}")
# username: username, {}
# } end
# end end
config.lograge.custom_options = lambda do |event| config.lograge.custom_options = lambda do |event|
exceptions = %w(controller action format id) begin
exceptions = %w(controller action format id)
params = event.payload[:params].except(*exceptions) params = event.payload[:params].except(*exceptions)
params[:files].map!(&:headers) if params[:files] params[:files].map!(&:headers) if params[:files]
output = { output = {
params: params.to_query, params: params.to_query,
database: RailsMultisite::ConnectionManagement.current_db, database: RailsMultisite::ConnectionManagement.current_db,
} }
if data = Thread.current[:_method_profiler] if data = Thread.current[:_method_profiler]
sql = data[:sql] sql = data[:sql]
if sql if sql
output[:db] = sql[:duration] * 1000 output[:db] = sql[:duration] * 1000
output[:db_calls] = sql[:calls] output[:db_calls] = sql[:calls]
end
redis = data[:redis]
if redis
output[:redis] = redis[:duration] * 1000
output[:redis_calls] = redis[:calls]
end
end end
redis = data[:redis] output
rescue => e
if redis Rails.logger.warn("Failed to append custom options: #{e.message}\n#{e.backtrace.join("\n")}")
output[:redis] = redis[:duration] * 1000 {}
output[:redis_calls] = redis[:calls]
end
end end
output
end end
if ENV["LOGSTASH_URI"] if ENV["LOGSTASH_URI"]