/usr/lib/ruby/vendor_ruby/lograge/log_subscriber.rb is in ruby-lograge 0.5.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 | require 'json'
require 'action_pack'
require 'active_support/core_ext/class/attribute'
require 'active_support/log_subscriber'
module Lograge
class RequestLogSubscriber < ActiveSupport::LogSubscriber
def process_action(event)
return if Lograge.ignore?(event)
payload = event.payload
data = extract_request(event, payload)
data = before_format(data, payload)
formatted_message = Lograge.formatter.call(data)
logger.send(Lograge.log_level, formatted_message)
end
def redirect_to(event)
Thread.current[:lograge_location] = event.payload[:location]
end
def unpermitted_parameters(event)
Thread.current[:lograge_unpermitted_params] ||= []
Thread.current[:lograge_unpermitted_params].concat(event.payload[:keys])
end
def logger
Lograge.logger.presence || super
end
private
def extract_request(event, payload)
payload = event.payload
data = initial_data(payload)
data.merge!(extract_status(payload))
data.merge!(extract_runtimes(event, payload))
data.merge!(extract_location)
data.merge!(extract_unpermitted_params)
data.merge!(custom_options(event))
end
def initial_data(payload)
{
method: payload[:method],
path: extract_path(payload),
format: extract_format(payload),
controller: payload[:controller],
action: payload[:action]
}
end
def extract_path(payload)
path = payload[:path]
index = path.index('?')
index ? path[0, index] : path
end
if ::ActionPack::VERSION::MAJOR == 3 && ::ActionPack::VERSION::MINOR.zero?
def extract_format(payload)
payload[:formats].first
end
else
def extract_format(payload)
payload[:format]
end
end
def extract_status(payload)
if (status = payload[:status])
{ status: status.to_i }
elsif (error = payload[:exception])
exception, message = error
{ status: get_error_status_code(exception), error: "#{exception}: #{message}" }
else
{ status: 0 }
end
end
def get_error_status_code(exception)
status = ActionDispatch::ExceptionWrapper.rescue_responses[exception]
Rack::Utils.status_code(status)
end
def custom_options(event)
Lograge.custom_options(event) || {}
end
def before_format(data, payload)
Lograge.before_format(data, payload)
end
def extract_runtimes(event, payload)
data = { duration: event.duration.to_f.round(2) }
data[:view] = payload[:view_runtime].to_f.round(2) if payload.key?(:view_runtime)
data[:db] = payload[:db_runtime].to_f.round(2) if payload.key?(:db_runtime)
data
end
def extract_location
location = Thread.current[:lograge_location]
return {} unless location
Thread.current[:lograge_location] = nil
{ location: location }
end
def extract_unpermitted_params
unpermitted_params = Thread.current[:lograge_unpermitted_params]
return {} unless unpermitted_params
Thread.current[:lograge_unpermitted_params] = nil
{ unpermitted_params: unpermitted_params }
end
end
end
|