Getting time to display with ruby Logger

H

Harnish Botadra

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
 
H

Harnish Botadra

Vikas said:
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
 
J

Joel VanderWerf

Harnish said:
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.
 

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