1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00
ruby--ruby/lib/logger/formatter.rb
Maciej Mensfeld 0fb1a67bda Reference process PID using the Process#pid
This change will allow formatter to run from Ractors other than main.
2020-09-29 11:50:46 -07:00

36 lines
760 B
Ruby

# frozen_string_literal: true
class Logger
# Default formatter for log messages.
class Formatter
Format = "%s, [%s#%d] %5s -- %s: %s\n"
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 || "%Y-%m-%dT%H:%M:%S.%6N ")
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