Logger datetime_format not respected

When creating a new logger, the datetime_format is not respected.

require ‘logger’
log = Logger.new(STDOUT)
log.datetime_format = “%Y-%m-%d %H:%M:%S”
log.info “#{log.datetime_format}”

outputs:

I, [2010-03-03 14:31:31#66665] INFO – : %Y-%m-%d %H:%M:%S

Tried in ruby 1.9.1 and 1.8.7 with same results. What am I missing?

Good Afternoon,

On Wed, Mar 3, 2010 at 1:35 PM, Karl [email protected] wrote:

Tried in ruby 1.9.1 and 1.8.7 with same results. What am I missing?

Ok, I’ll play along - what exactly where you expecting? You seem to have
gotten exactly what you requested? Are you assuming that %H is hours on
a 12
hour clock? If so you are looking for %I - %H is 24 hour clock (
http://ruby-doc.org/core/classes/Time.html#M000298)

John

My fault, copied the wrong version:

require ‘logger’
log = Logger.new(STDOUT)
log.datetime_format = “%Y-%m-%d %H:%M:%S”
log.formatter = proc { |severity, datetime, progname, msg|
“[#{datetime}] #{msg}\n”
}
log.info “#{log.datetime_format}”

outputs:

[Wed Mar 03 15:01:05 -0700 2010] %Y-%m-%d %H:%M:%S

Figured it out. When you override the formatter, it’s not passing
along the datetime formated to the formatter. There is a
Formatter.format_datetime or format the datetime object yourself.

That’s what I get for copying examples.

Afternoon again,

On Wed, Mar 3, 2010 at 2:05 PM, Karl [email protected] wrote:

outputs:

[Wed Mar 03 15:01:05 -0700 2010] %Y-%m-%d %H:%M:%S

So what you’ve done is changed the format but then changed the formatter
so
it never uses the datetime_format

Change your proc code to this and you should be ok…

log.formatter = proc { |severity, datetime, progname, msg|
“[#{datetime.strftime(log.datetime_format)}] #{msg}\n”
}

John

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs