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
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.
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.
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.
try this:
"#{severity} [#{datetime.strftime('%Y-%m-%d %H:%M:%S.%L')}]: #{msg}\n"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With