Class: Console::Adapter::Rails::ActionController::LogSubscriber

Inherits:
ActiveSupport::LogSubscriber
  • Object
show all
Defined in:
lib/console/adapter/rails/action_controller.rb

Overview

A Rails log subscriber which is compatible with ‘Console::Logger`. It receives events from `ActiveSupport::Notifications` and logs them to the console.

Instance Method Summary collapse

Instance Method Details

#process_action(event) ⇒ Object

Log an ActionController ‘process_action` event.

Includes the following fields:

  • ‘subject`: “process_action.action_controller”

  • ‘controller`: The name of the controller.

  • ‘action`: The action performed.

  • ‘format`: The format of the response.

  • ‘method`: The HTTP method of the request.

  • ‘path`: The path of the request.

  • ‘status`: The HTTP status code of the response.

  • ‘view_runtime`: The time spent rendering views in milliseconds.

  • ‘db_runtime`: The time spent querying the database in milliseconds.

  • ‘location`: The redirect location if any.

  • ‘allocations`: The number of allocations performed.

  • ‘duration`: The total time spent processing the request in milliseconds.



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
# File 'lib/console/adapter/rails/action_controller.rb', line 32

def process_action(event)
  payload = event.payload.dup
  
  # This may contain sensitive information:
  params = payload.delete(:params)
  
  # These objects are not useful and may not serialize correctly:
  headers = payload.delete(:headers)
  request = payload.delete(:request)
  response = payload.delete(:response)
  
  if request and ip = request.remote_ip
    payload[:source_address] = ip
  end
  
  if response and headers = response.headers
    # Extract redirect location if any:
    location = response.headers['Location'] || response.headers['location']
    if location
      payload[:location] = location
    end
  end
  
  payload[:allocations] = event.allocations
  payload[:duration] = event.duration
  
  Console.logger.info(event.name, **payload)
end