class Sapience::Extensions::ActionController::LogSubscriber

Public Instance Methods

orig_process_action(event)
Alias for: process_action
orig_start_processing(event)
Alias for: start_processing
process_action(event) click to toggle source
# File lib/sapience/extensions/action_controller/log_subscriber.rb, line 16
def process_action(event)
  return unless logger.info?
  data = request(event.payload)
  data.merge! request_id(event)
  data.merge! runtimes(event)
  data.merge! exception(event.payload)
  info(data)
end
Also aliased as: orig_process_action
start_processing(event) click to toggle source

Log as debug to hide Processing messages in production

# File lib/sapience/extensions/action_controller/log_subscriber.rb, line 12
def start_processing(event)
  debug { "Processing ##{event.payload[:action]}" }
end
Also aliased as: orig_start_processing

Private Instance Methods

exception(payload) click to toggle source

Monkey patching to enable exception logging

# File lib/sapience/extensions/action_controller/log_subscriber.rb, line 68
def exception(payload)
  if payload[:exception]
    exception, message = payload[:exception]
    message ||= exception.message
    status = ActionDispatch::ExceptionWrapper.status_code_for_exception(exception)
    backtrace = $ERROR_INFO.try(:backtrace).try(:first)
    backtrace ||= exception.backtrace.first
    message = "#{exception}\n#{message}\n#{backtrace}"
    { status: status, error: message }
  else
    {}
  end
end
format(payload) click to toggle source
# File lib/sapience/extensions/action_controller/log_subscriber.rb, line 43
def format(payload)
  if ::ActionPack::VERSION::MAJOR == 3 && ::ActionPack::VERSION::MINOR == 0
    payload[:formats].first
  else
    payload[:format]
  end
end
request(payload) click to toggle source
# File lib/sapience/extensions/action_controller/log_subscriber.rb, line 27
def request(payload) # rubocop:disable AbcSize
  {
    method: payload[:method].upcase,
    request_path: request_path(payload),
    format: format(payload),
    status: payload[:status].to_i,
    controller: payload[:params]["controller"],
    action: payload[:params]["action"],
    host: Sapience.config.host,
    route: "#{payload[:params].delete("controller")}##{payload[:params]["action"]}",
    message: "Completed ##{payload[:params].delete("action")}",
    tags: Sapience.tags,
    params: payload[:params],
  }
end
request_id(event) click to toggle source
# File lib/sapience/extensions/action_controller/log_subscriber.rb, line 51
def request_id(event)
  { request_id: event.transaction_id }
end
request_path(payload) click to toggle source
# File lib/sapience/extensions/action_controller/log_subscriber.rb, line 82
def request_path(payload)
  payload[:path].split("?").first
end
runtimes(event) click to toggle source
# File lib/sapience/extensions/action_controller/log_subscriber.rb, line 55
def runtimes(event)
  {
    total: event.duration,
    view: event.payload[:view_runtime],
    db: event.payload[:db_runtime],
  }.each_with_object({}) do |(name, runtime), runtimes|
    runtimes[:runtimes] ||= {}
    runtimes[:runtimes][name] = runtime.to_f.round(2) if runtime
    runtimes
  end
end