Better logging in Rails in production mode

I’m quite surprised, that even of version 2.2.2 of Rails, the logs are written in a way that makes them almost useless, as there’s no time stamp or severity level.
The solution I’ve come up with, is (instead of monkey patching the BufferedLogger) to create a new type of logger which inherits from the BufferedLogger and then change the ‘add’ method in order to produce nicer logs.
This file is lib/audit_logger.rb :

class AuditLogger < ActiveSupport::BufferedLogger
  SEVERITIES = Severity.constants.inject([]) {|arr,c| arr[Severity.const_get(c)] = c; arr}
  # Redefine the ActiveSupport::BufferedLogger#add
  def add(severity, message = nil, progname = nil, &block)
    return if @level > severity
    message = (message || (block && || progname).to_s
    # This is the line that was changed:
    message = "#{} #{SEVERITIES[severity]} #{message.strip}\n"
    buffer << message

Then, in config/environments/production.rb you need to add:

require 'audit_logger'
config.logger =
config.logger.level = AuditLogger::INFO #or other level
# This is what Rails does by default in production mode when using the default logger
config.logger.auto_flushing = false

2 Responses to “Better logging in Rails in production mode”

  1. Witold Rugowski Says:

    config.logger.auto_flushing = false does not what Rails by default. Deafult would be auto_flushing=1 – flush log to disk after each message.

    Setting auto_flushing to false will stop auto flushing and You will need manually call flush to save entries or memory will end at some point 😉


    Roman Reply:

    Please see line 489:


Leave a Reply