Method: Logger#formatter
- Defined in:
- lib/logger.rb
#formatter ⇒ Object
Sets or retrieves the logger entry formatter proc.
When formatter is nil, the logger uses Logger::Formatter.
When formatter is a proc, a new entry is formatted by the proc, which is called with four arguments:
-
severity: The severity of the entry. -
time: A Time object representing the entry’s timestamp. -
progname: The program name for the entry. -
msg: The message for the entry (string or string-convertible object).
The proc should return a string containing the formatted entry.
This custom formatter uses String#dump to escape the message string:
logger = Logger.new($stdout, progname: 'mung')
original_formatter = logger.formatter || Logger::Formatter.new
logger.formatter = proc { |severity, time, progname, msg|
original_formatter.call(severity, time, progname, msg.dump)
}
logger.add(Logger::INFO, "hello \n ''")
logger.add(Logger::INFO, "\f\x00\xff\\\"")
Output:
I, [2022-05-13T13:16:29.637488 #8492] INFO -- mung: "hello \n ''"
I, [2022-05-13T13:16:29.637610 #8492] INFO -- mung: "\f\x00\xFF\\\""
473 474 475 |
# File 'lib/logger.rb', line 473 def formatter @formatter end |