class Lograge::RequestLogSubscriber

Public Instance Methods

logger() click to toggle source
Calls superclass method
# File lib/lograge/log_subscriber.rb, line 28
def logger
  Lograge.logger.presence || super
end
process_action(event) click to toggle source
# File lib/lograge/log_subscriber.rb, line 9
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
redirect_to(event) click to toggle source
# File lib/lograge/log_subscriber.rb, line 19
def redirect_to(event)
  RequestStore.store[:lograge_location] = event.payload[:location]
end
unpermitted_parameters(event) click to toggle source
# File lib/lograge/log_subscriber.rb, line 23
def unpermitted_parameters(event)
  RequestStore.store[:lograge_unpermitted_params] ||= []
  RequestStore.store[:lograge_unpermitted_params].concat(event.payload[:keys])
end

Private Instance Methods

before_format(data, payload) click to toggle source
# File lib/lograge/log_subscriber.rb, line 95
def before_format(data, payload)
  Lograge.before_format(data, payload)
end
custom_options(event) click to toggle source
# File lib/lograge/log_subscriber.rb, line 90
def custom_options(event)
  options = Lograge.custom_options(event) || {}
  options.merge event.payload[:custom_payload] || {}
end
extract_format(payload) click to toggle source
# File lib/lograge/log_subscriber.rb, line 65
def extract_format(payload)
  payload[:formats].first
end
extract_location() click to toggle source
# File lib/lograge/log_subscriber.rb, line 106
def extract_location
  location = RequestStore.store[:lograge_location]
  return {} unless location

  RequestStore.store[:lograge_location] = nil
  { location: strip_query_string(location) }
end
extract_path(payload) click to toggle source
# File lib/lograge/log_subscriber.rb, line 54
def extract_path(payload)
  path = payload[:path]
  strip_query_string(path)
end
extract_request(event, payload) click to toggle source
# File lib/lograge/log_subscriber.rb, line 34
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
extract_runtimes(event, payload) click to toggle source
# File lib/lograge/log_subscriber.rb, line 99
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
extract_status(payload) click to toggle source
# File lib/lograge/log_subscriber.rb, line 74
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
extract_unpermitted_params() click to toggle source
# File lib/lograge/log_subscriber.rb, line 114
def extract_unpermitted_params
  unpermitted_params = RequestStore.store[:lograge_unpermitted_params]
  return {} unless unpermitted_params

  RequestStore.store[:lograge_unpermitted_params] = nil
  { unpermitted_params: unpermitted_params }
end
get_error_status_code(exception) click to toggle source
# File lib/lograge/log_subscriber.rb, line 85
def get_error_status_code(exception)
  status = ActionDispatch::ExceptionWrapper.rescue_responses[exception]
  Rack::Utils.status_code(status)
end
initial_data(payload) click to toggle source
# File lib/lograge/log_subscriber.rb, line 44
def initial_data(payload)
  {
    method: payload[:method],
    path: extract_path(payload),
    format: extract_format(payload),
    controller: payload[:controller],
    action: payload[:action]
  }
end
strip_query_string(path) click to toggle source
# File lib/lograge/log_subscriber.rb, line 59
def strip_query_string(path)
  index = path.index('?')
  index ? path[0, index] : path
end