Class: ApiHammer::RequestLogSubscriber

Inherits:
ActiveSupport::LogSubscriber
  • Object
show all
Defined in:
lib/api_hammer/rails_request_logging.rb

Instance Method Summary collapse

Instance Method Details

#process_action(event) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/api_hammer/rails_request_logging.rb', line 28

def process_action(event)
  if event.payload[:request_env]
    info = (event.payload[:request_env]['request_logger.info'] ||= {})
  else
    # if an exception occurs in the action, append_info_to_payload isn't called and 
    # event.payload[:request_env] doesn't get set. fall back to use Thread.current.
    info = (Thread.current['request_logger.info'] ||= {})
  end
  info.update(event.payload.slice(:controller, :action, :exception, :format, :formats, :view_runtime, :db_runtime))
  info.update(:transaction_id => event.transaction_id)
  info.update(event.payload['request_logger.info']) if event.payload['request_logger.info']
  # if there is an exception, ActiveSupport saves the class and message but not backtrace. but this 
  # gets called from an ensure block, so $! is set - retrieve the backtrace from that.
  if $!
    # this may be paranoid - it should always be the case that what gets set in :exception is the 
    # same as the current error, but just in case it's not, we'll put the current error somewhere else
    if info[:exception] == [$!.class.to_s, $!.message]
      info[:exception] += [$!.backtrace]
    else
      info[:current_exception] = [$!.class.to_s, $!.message, $!.backtrace]
    end
  end
end