# Getting time to display with ruby Logger

**URL:** <https://www.ruby-forum.com/t/getting-time-to-display-with-ruby-logger/172069>\
**Category:** Ruby\
**Created:** [July 14, 2009, 5:03am UTC](https://www.ruby-forum.com/t/getting-time-to-display-with-ruby-logger/172069 "2009-07-14T05:03:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![hbotadra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/e9bcb4/32.png) [@hbotadra](https://www.ruby-forum.com/u/hbotadra)\
**Post date:** [July 14, 2009, 5:03am UTC](https://www.ruby-forum.com/t/getting-time-to-display-with-ruby-logger/172069/1 "2009-07-14T05:03:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![hbotadra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/e9bcb4/32.png) [@hbotadra](https://www.ruby-forum.com/u/hbotadra)\
**Post date:** [July 14, 2009, 6:33am UTC](https://www.ruby-forum.com/t/getting-time-to-display-with-ruby-logger/172069/2 "2009-07-14T06:33:53Z")

</div>

Hi,

You could try  
[http://ruby-doc.org/core/classes/Time.html](http://ruby-doc.org/core/classes/Time.html)

regards,  
Vikas Sarin

---

<div class="post-metadata">

**Author:** ![hbotadra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/e9bcb4/32.png) [@hbotadra](https://www.ruby-forum.com/u/hbotadra)\
**Post date:** [July 14, 2009, 6:36am UTC](https://www.ruby-forum.com/t/getting-time-to-display-with-ruby-logger/172069/3 "2009-07-14T06:36:47Z")

</div>

Vikas Sarin wrote:

> Hi,
> 
> You could try  
> [class Time - RDoc Documentation](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

---

<div class="post-metadata">

**Author:** ![hbotadra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/e9bcb4/32.png) [@hbotadra](https://www.ruby-forum.com/u/hbotadra)\
**Post date:** [July 14, 2009, 6:56am UTC](https://www.ruby-forum.com/t/getting-time-to-display-with-ruby-logger/172069/4 "2009-07-14T06:56:05Z")

</div>

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.
