1

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.

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.