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
--
Posted via http://www.ruby-forum.com/.

Hi,

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

regards,
Vikas Sarin

···

-----Original Message-----
From: harnish_544@yahoo.com [mailto:harnish_544@yahoo.com]
Sent: Tuesday, July 14, 2009 8:34 AM
To: ruby-talk ML
Subject: 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
--
Posted via http://www.ruby-forum.com/.

Harnish Botadra 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.

···

--
       vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Vikas Sarin wrote:

Hi,

You could try
class Time - RDoc Documentation

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

···

--
Posted via http://www.ruby-forum.com/\.