Getting time to display with ruby Logger

Hey guys,

I am having trouble displaying log messages with timestamps. My simple
code looks like:


require ‘logger’

logger = Logger.new("/tmp/tmp.log")

logger.level = Logger::DEBUG
logger.datetime_format = “%Y-%m-%d %H:%M:%S”

logger.info “Testing!”

All I get in my /tmp/tmp.log is:


$ tail -f /tmp/tmp.log

Logfile created on Mon Jul 13 19:42:20 -0700 2009 by /

Testing!

How, do I get to display something like:

I, [2008-03-18T17:09:29.216000 #2020] DEBUG – : Testing

Any pointers would be greatly appreciated.

Thanks,
Harnish

Hi,

You could try
http://ruby-doc.org/core/classes/Time.html

regards,
Vikas Sarin

Vikas Sarin wrote:

Hi,

You could try
http://ruby-doc.org/core/classes/Time.html

regards,
Vikas Sarin

That’s not what I am looking for. I want to use the Logger class to
spit out the timestamp for me. The utility is built-in by default and I
don’t want prepend each and every log statement with a timestamp using
the Time class.

Long story short, I my question really is, why doesn’t using Logger give
me the timestamp I need in my log messages?

Thanks,
Harnish

Harnish B. wrote:

How, do I get to display something like:

I, [2008-03-18T17:09:29.216000 #2020] DEBUG – : Testing

Your code outputs in that format for me, on ruby-1.8.6. Does it make a
difference if you remove the line

logger.datetime_format = “%Y-%m-%d %H:%M:%S”

?

That will get you back to the default, which displays microseconds, as
you wanted anyway.

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