class Loggerator::Railtie::LogSubscriber

Constants

FIELDS

Public Instance Methods

process_action(event) click to toggle source
# File lib/loggerator/railtie/log_subscriber.rb, line 15
def process_action(event)
  exception = event.payload[:exception]
  if exception.present?
    # In Rails 3.2.9 event.payload[:exception] was changed from an
    # Exception object to an Array containing the e.class.name and
    # e.message. Adding handling for this case here.
    if exception.is_a?(Array)
      exception_class_name, exception_message = exception
      exception = exception_class_name.constantize.new(exception_message)
    end

    self.log_error(exception, status: 500)
  else
    self.log(extract_request_data_from_event(event))
  end
end
redirect_to(event) click to toggle source
# File lib/loggerator/railtie/log_subscriber.rb, line 32
def redirect_to(event)
  Thread.current[:scrolls_rails_location] = event.payload[:location]
end

Private Instance Methods

extract_request(payload) click to toggle source
# File lib/loggerator/railtie/log_subscriber.rb, line 46
def extract_request(payload)
  {
    method:     payload[:method],
    path:       payload[:path],
    format:     payload[:format],
    controller: payload[:params]["controller"],
    action:     payload[:params]["action"]
  }
end
extract_request_data_from_event(event) click to toggle source
# File lib/loggerator/railtie/log_subscriber.rb, line 38
def extract_request_data_from_event(event)
  data          = extract_request(event.payload)
  data[:status] = extract_status(event.payload)

  data.merge!(runtimes(event))
  data.merge!(location(event))
end
extract_status(payload) click to toggle source
# File lib/loggerator/railtie/log_subscriber.rb, line 56
def extract_status(payload)
  if payload[:status]
    payload[:status].to_i
  else
    0
  end
end
location(_event) click to toggle source
# File lib/loggerator/railtie/log_subscriber.rb, line 74
def location(_event)
  if location = Thread.current[:scrolls_rails_location]
    Thread.current[:scrolls_rails_location] = nil

    { location: location }
  else
    {}
  end
end
runtimes(event) click to toggle source
# File lib/loggerator/railtie/log_subscriber.rb, line 64
def runtimes(event)
  { duration: event.duration,
    view:     event.payload[:view_runtime],
    db:       event.payload[:db_runtime]
  }.inject({}) do |runtimes, (name, runtime)|
    runtimes[name] = runtime.to_f.round(2) if runtime
    runtimes
  end
end