Forum: Ruby on Rails ActiveRecord changes Logger formatting

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
9c473e546ce5c80e6f39d7e0502b5561?d=identicon&s=25 Rob Redmon (rredmon)
on 2009-05-24 15:19
(Received via mailing list)
Why does my Logger formatting change after I "require
'active_record'"?

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

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
applications.
9c473e546ce5c80e6f39d7e0502b5561?d=identicon&s=25 Rob Redmon (rredmon)
on 2009-05-30 10:46
(Received via mailing list)
Does anyone have any ideas here???  I no longer have useful Logger
messages.  That's very frustrating.

R
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-05-30 11:54
(Received via mailing list)
On May 30, 9:45 am, RobR <rob.webina...@gmail.com> 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
does

http://github.com/rails/rails/blob/669fd84910586d4...

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

Fred
0f50b9a2ad85666d537d39bda49327ee?d=identicon&s=25 Jonathan Rochkind (jrochkind)
on 2009-06-01 04:43
Frederick Cheung wrote:
> probably because activerecord requires activesupport and activesupport
> does
>
> 
http://github.com/rails/rails/blob/669fd84910586d4...
>

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:

http://umlaut.rubyforge.org/svn/trunk/lib/umlaut_logger.rb

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

    require_dependency 'umlaut_logger'
    severity_level =
ActiveSupport::BufferedLogger.const_get(config.log_level.to_s.upcase)
    log_file = File.join(RAILS_ROOT, "log", "#{RAILS_ENV}.log")

    our_logger = UmlautLogger.new(log_file, severity_level)
    our_logger.formatter = lambda do |severity_label, msg|
      time_fmtd = Time.now.strftime("%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)
    end
    config.logger = our_logger
This topic is locked and can not be replied to.