mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
a8b11b5cdd
Formatting a datetime should only pertain to itself and valid datetimes do not contain a space. Should there be a desire to show show a space between the datetime and the process pid in the formatted log, this formatting logic should take place there. Furthermore, the default datetime format is moved to a class variable to allowing this variable to be overwritten by subclasses. https://github.com/ruby/logger/commit/7cbd434349
37 lines
796 B
Ruby
37 lines
796 B
Ruby
# frozen_string_literal: true
|
|
|
|
class Logger
|
|
# Default formatter for log messages.
|
|
class Formatter
|
|
Format = "%s, [%s #%d] %5s -- %s: %s\n"
|
|
DatetimeFormat = "%Y-%m-%dT%H:%M:%S.%6N"
|
|
|
|
attr_accessor :datetime_format
|
|
|
|
def initialize
|
|
@datetime_format = nil
|
|
end
|
|
|
|
def call(severity, time, progname, msg)
|
|
Format % [severity[0..0], format_datetime(time), Process.pid, severity, progname,
|
|
msg2str(msg)]
|
|
end
|
|
|
|
private
|
|
|
|
def format_datetime(time)
|
|
time.strftime(@datetime_format || DatetimeFormat)
|
|
end
|
|
|
|
def msg2str(msg)
|
|
case msg
|
|
when ::String
|
|
msg
|
|
when ::Exception
|
|
"#{ msg.message } (#{ msg.class })\n#{ msg.backtrace.join("\n") if msg.backtrace }"
|
|
else
|
|
msg.inspect
|
|
end
|
|
end
|
|
end
|
|
end
|