Logger datetime_format not respected

K

Karl

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?
 
J

John W Higgins

[Note: parts of this message were removed to make it a legal post.]

Good Afternoon,

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?
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
 
K

Karl

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
 
J

John W Higgins

[Note: parts of this message were removed to make it a legal post.]

Afternoon again,

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
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
 
K

Karl

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.
 

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,416
Latest member
LionelQ387

Latest Threads

Top