Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to format ruby logger to this?

Tags:

logging

ruby

The default style in ruby logger is:

SeverityID, [Date Time mSec #pid] SeverityLabel -- ProgName: message
# => D, [2013-11-25T13:31:03.451024 #38180] DEBUG -- : <message...>

and I want to make it looks like:

SeverityLabel [Date Time mSec #pid]: message
# => DEBUG [2013-11-25T13:31:03.451024 #38180]: <message...>

I know I can format it like this:

logger.formatter = proc do |severity, datetime, progname, msg|
   "severity [#{datetime}]: #{msg}\n"
end
# => DEBUG [2013-11-25 13:37:45 -0800]: <message...>

but the datetime in proc does NOT look like what it showed in default. I've tried using datetime_format

logger.datetime_format = "%Y-%m-%d %H:%M:%S.%L"

but it has NO effect on my logger...

Also, I cannot find #pid either

any thoughts?

I've seen the following post:

How to format ruby logger?

and the doc:

http://www.ruby-doc.org/stdlib-1.9.3/libdoc/logger/rdoc/Logger.html


Thanks to Some Guy's reply, This is what I ended up doing:

logger.formatter = proc do |severity, datetime, progname, msg|
   "#{severity} [#{datetime.strftime('%Y-%m-%d %H:%M:%S.%6N')} ##{Process.pid}]: #{msg}\n"
end
like image 445
Yuan Hsueh Avatar asked Nov 25 '13 21:11

Yuan Hsueh


People also ask

How do I create a log file in Ruby?

Since the logger library comes with Ruby, there's no need to install any gems or other libraries. To begin using the logger library, simply require 'logger' and create a new Logger object. Any messages written to the Logger object will be written to the log file.

Where does Rails logger write to?

In a Rails app, logs are stored under the /log folder. In development mode, the development. log file is used & you see log output on the terminal you're running rails server on.

How do I check logs in Ruby on Rails?

2.2 Log Levels If you want to know the current log level, you can call the Rails. logger. level method. This is useful when you want to log under development or staging without flooding your production log with unnecessary information.


1 Answers

try this:

 "#{severity} [#{datetime.strftime('%Y-%m-%d %H:%M:%S.%L')}]: #{msg}\n"
like image 199
Some Guy Avatar answered Oct 05 '22 19:10

Some Guy