talira

Taming Rails' default request logging

open sourceMITself-hostablefree

Lograge replaces Rails' multi-line request logging with a single line per request that holds the method, path, format, controller, action, status and timings for the whole request, the views and the database. It unhooks Rails' ActionController and ActionView log subscribers and listens for the process_action and redirect_to events itself, and it also quiets Action Cable's logs. The default output is Heroku-router style key=value pairs, with formatters for JSON, Logstash, Graylog, CEE, LTSV and raw hashes, and hooks to add fields from the request or controller or to skip actions. Mathias Meyer extracted it from Travis CI in 2012.

Problems it solves

  • my Rails production log spits out eight lines per request and they get interleaved across processes
  • get one line per HTTP request in Rails with status, duration and controller action
  • make Rails request logs parseable by our log aggregator instead of free-form text
  • write Rails request logs as JSON so I can search them by field
  • add the user id and host to every request log line in my Rails app
  • stop logging health check requests from the load balancer
  • the 'Rendered partial' lines drown out everything useful in our Rails logs
  • ship Rails request logs to Logstash or Graylog in a format they understand
  • see how much of a request's time went to the database versus the views at a glance
  • Action Cable floods the log with lines nobody reads
  • log the exception class and message on the same line as the failed request
  • key=value request logs like the Heroku router writes, from my own Rails app

Features

  • one log line per request with method, path, format, controller, action, status and durations
  • view and database timings in the request line
  • key=value (default), JSON, Logstash, Graylog2, CEE, LTSV, lines, deep key=value and raw hash formatters
  • custom formatter: any object that responds to call
  • custom_options and custom_payload hooks to add fields from the event or the controller
  • ignore_actions and ignore_custom to skip requests
  • keep_original_rails_log to keep Rails' own log alongside
  • Action Cable support (perform_action, subscribe, unsubscribe, connect, disconnect)
  • configurable base controller classes, including ActionController::API
  • removes the 'Started GET' line and rack-cache's verbose output

Limitations

  • Doesn't log request parameters by default: add them yourself through custom_options, filtering sensitive values1
  • Drops view and partial rendering times from the log: it removes ActionView logging entirely1

Capabilities

Has an APIplan not known

Alternatives

The same job

These do what Lograge does.

  1. LogStruct Zero-config, type-safe JSON logging for Ruby on Rails apps

Compare Lograge with the top 3