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

G

Georges Ko

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
 
N

Nick Dainty

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.
 
N

Nick Dainty

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.
 
G

Gavin Sinclair

Georges said:
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: [code snipped]

$ ruby testlog.rb
Hello, world!

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
 
G

Georges Ko

Nick Dainty said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,159
Messages
2,570,879
Members
47,417
Latest member
DarrenGaun

Latest Threads

Top