Ruby's Logger vs ActiveRecord: no more Logger data output

Hi,

I’m writing an application that uses both Ruby’s Logger and
ActiveRecord, but ActiveRecord seems to break Ruby’s Logger, as
the Logger data (date, level, etc…) is not output anymore.

Below is an example:

$ cat testlog.rb
require “logger”
require “rubygems”
require_gem “activerecord”

log = Logger.new(STDOUT)
log.level = Logger::DEBUG

log.info(“Hello, world!”)
$ ruby testlog.rb
Hello, world!

versus

$ ruby testlog2.rb
require “logger”
require “rubygems”
######### require_gem “activerecord”

log = Logger.new(STDOUT)
log.level = Logger::DEBUG

log.info(“Hello, world!”)

$ ruby testlog2.rb
I, [2006-07-12T18:26:13.556000 #4228] INFO – : Hello, world!

To make it normal, I have to load “logger” after require_gem
“activerecord” (with warning messages output)…

What do I miss?

Georges

I’m writing an application that uses both Ruby’s Logger and
ActiveRecord, but ActiveRecord seems to break Ruby’s Logger, as
the Logger data (date, level, etc…) is not output anymore.

Hi George

Rails’ ActiveSupport overrides the Logger#format_message method. To
get back to the normal log formatting, you can use something like this
in you script.

class Logger
private

Rails overrides this method so that it can customize the format

of it’s logs.

def format_message(*args)
old_format_message(*args)
end
end

Which is a bit hacky maybe, but it’s working for me at the moment.

Hope this helps.

Nick,
Funny, I was just having this same problem with Logger and ActiveRecord.
I
can’t seem to get your solution to work.

This script:

require ‘logger’
require ‘rubygems’
require_gem ‘activerecord’

class Logger
private

Rails overrides this method so that it can customize the format

of it’s logs.

def format_message(*args)
old_format_message(*args)
end
end

log = Logger.new(STDERR)
log.info(“Hello!”)

Throws this error:

/home/neilk/bin/loggertest.rb:14:in format_message': undefined methodold_format_message’ for #Logger:0x489cc8 (NoMethodError)
from /ropt/local/lib/ruby/1.8/logger.rb:320:in add' from /ropt/local/lib/ruby/1.8/logger.rb:372:ininfo’
from /home/neilk/bin/loggertest.rb:21

What am I missing?

Georges Ko wrote:

versus

$ ruby testlog2.rb
I, [2006-07-12T18:26:13.556000 #4228] INFO – : Hello, world!

To make it normal, I have to load “logger” after require_gem
“activerecord” (with warning messages output)…

What do I miss?

I think Rails defines a class called Logger which must be clobbering
the standard one.

Gavin

Hi, Nick

Funny, I was just having this same problem with Logger and ActiveRecord. I

can’t seem to get your solution to work.

Have a look at clean_logger.rb in active support. In my copy of rails
(latest stable branch) it does this:

alias old_format_message format_message

Which is what makes my example work.

An old version of rails was the problem – I was testing on a machine
that
version 1.0.0. Works perfectly on machine with 1.1.x rails.

Thanks!

Funny, I was just having this same problem with Logger and ActiveRecord. I
can’t seem to get your solution to work.

Have a look at clean_logger.rb in active support. In my copy of rails
(latest stable branch) it does this:

alias old_format_message format_message

Which is what makes my example work.

“Nick Dainty” [email protected] writes:

Have a look at clean_logger.rb in active support. In my copy of rails
(latest stable branch) it does this:

alias old_format_message format_message

OK, thanks!

Georges