Forum: Ruby on Rails ActiveRecord changes Logger formatting

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Rob R. (Guest)
on 2009-05-24 17:19
(Received via mailing list)
Why does my Logger formatting change after I "require

irb(main):001:0> require 'logger'
=> true
irb(main):002:0> $logger = Logger::new STDOUT
=> #<Logger:0xb7dd0a38 @formatter=nil,
@datetime_format=nil>, @level=0, @progname=nil,
@logdev=#<Logger::LogDevice:0xb7dd09e8 @shift_age=nil, @filename=nil,
@mon_waiting_queue=[], @mon_entering_queue=[], @mon_count=0,
@mon_owner=nil>, @dev=#<IO:0xb7f10574>, @shift_size=nil>>
irb(main):004:0* ${ "Expected log line style" }
I, [2009-05-24T06:58:44.599720 #22050]  INFO -- : Expected log line
=> true
irb(main):006:0* require 'active_record'
=> true
irb(main):007:0> ${ "Why did AR change my log line
style?" }
Why did AR change my log line style?
=> true

Furthermore, how does one specify a user defined Logger format style?
I need to provide my Log lines in a format consistent with our other
Rob R. (Guest)
on 2009-05-30 12:46
(Received via mailing list)
Does anyone have any ideas here???  I no longer have useful Logger
messages.  That's very frustrating.

Frederick C. (Guest)
on 2009-05-30 13:54
(Received via mailing list)
On May 30, 9:45 am, RobR <removed_email_address@domain.invalid> wrote:
> Does anyone have any ideas here???  I no longer have useful Logger
> messages.  That's very frustrating.
probably because activerecord requires activesupport and activesupport

(note that it defaults you to Logger::SimpleFormatter)

Jonathan R. (Guest)
on 2009-06-01 06:43
Frederick C. wrote:
> probably because activerecord requires activesupport and activesupport
> does

For what it's worth, I share the original questioners general
unhappiness with Rails default logging format.

It's possible to customize that without too much code, although somewhat
more confusing and under-documented than one might wish, and also seems
to change slightly from one Rails version to the next, for whatever
reasons the precise hooks into Rails logging mechanisms seem to be
somewhat unstable.

Here's how I customize logging in a Rails 2.1.2 app, choosing to use the
Rails2 default "BuferredLogger" for what (I assume) is better efficiency
in disk writes, but with my own output formats.  Anyone feel free to let
me know if there's a better way, or if this is easier in subsequent
Rails versions. But it's not too hard (once you figure it out, which
took me a bit of playing around).

In my lib directory, a sub-class of the BufferedLogger that actually
accepts a formatter, as the standard BufferedLogger (at least in 2.1.1)
oddly does not:

Then in environment.rb, set the logger and it's formatter:

    require_dependency 'umlaut_logger'
    severity_level =
    log_file = File.join(RAILS_ROOT, "log", "#{RAILS_ENV}.log")

    our_logger =, severity_level)
    our_logger.formatter = lambda do |severity_label, msg|
      time_fmtd ="%d %b %H:%M:%S")
      preface = "[#{time_fmtd}] (pid:#{$$}) #{severity_label}: "
      # stick our preface AFTER any initial newlines
      msg =~ /^(\n+)[^\n]/
      index = $1 ? $1.length : 0
      return msg.insert(index, preface)
    config.logger = our_logger
This topic is locked and can not be replied to.