Reputation: 5070
I want to change only the msg
part of the default_formatter in Ruby 1.9.3 Logger.
How can I do this?
I know that I can do
logger.formatter = proc do |severity, datetime, progname, msg|
"[mymessage] #{msg}\n"
end
but then I lose the other default formatting of severity, datetime, progname
.
Ideally I'd like to just do msg = "[mymessage] " + msg
and then execute the default formatter with this new msg
.
Upvotes: 1
Views: 496
Reputation: 18762
Based on the example in documentation, one can do the following:
require 'logger'
logger = Logger.new(STDOUT)
original_formatter = Logger::Formatter.new
logger.formatter = proc { |severity, datetime, progname, msg|
original_formatter.call(severity, datetime, progname, "[mymessage] #{msg}\n")
}
logger.debug("I am a debug msg")
logger.info("I am an info msg")
Output of program:
D, [2016-01-13T12:45:47.354261 #11512] DEBUG -- : [mymessage] I am a debug msg
I, [2016-01-13T12:45:47.354261 #11512] INFO -- : [mymessage] I am an info msg
Upvotes: 1