2019-03-21 12:14:14 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-12-11 22:08:35 -05:00
|
|
|
class Logger
|
|
|
|
# Default formatter for log messages.
|
|
|
|
class Formatter
|
2021-07-29 04:31:33 -04:00
|
|
|
Format = "%s, [%s #%d] %5s -- %s: %s\n"
|
|
|
|
DatetimeFormat = "%Y-%m-%dT%H:%M:%S.%6N"
|
2018-12-11 22:08:35 -05:00
|
|
|
|
|
|
|
attr_accessor :datetime_format
|
|
|
|
|
|
|
|
def initialize
|
|
|
|
@datetime_format = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def call(severity, time, progname, msg)
|
2020-09-28 09:20:35 -04:00
|
|
|
Format % [severity[0..0], format_datetime(time), Process.pid, severity, progname,
|
2018-12-11 22:08:35 -05:00
|
|
|
msg2str(msg)]
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def format_datetime(time)
|
2021-07-29 04:31:33 -04:00
|
|
|
time.strftime(@datetime_format || DatetimeFormat)
|
2018-12-11 22:08:35 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def msg2str(msg)
|
|
|
|
case msg
|
|
|
|
when ::String
|
|
|
|
msg
|
|
|
|
when ::Exception
|
2019-07-31 01:38:38 -04:00
|
|
|
"#{ msg.message } (#{ msg.class })\n#{ msg.backtrace.join("\n") if msg.backtrace }"
|
2018-12-11 22:08:35 -05:00
|
|
|
else
|
|
|
|
msg.inspect
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|