Created
June 4, 2020 16:50
-
-
Save copiousfreetime/15ee67dafeaed2151af23b45f1af6c7c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'logger' | |
class LogFormatter | |
MSG_FORMAT = "%s -- %5s : %s\n".freeze | |
DATE_TIME_FORMAT = "%Y-%m-%dT%H:%M:%S.%3N".freeze | |
attr_accessor :datetime_format | |
def initialize | |
@datetime_format = nil | |
end | |
def call(severity, time, progname, msg) | |
MSG_FORMAT % [ format_datetime(time), severity, msg2str(msg)] | |
end | |
private | |
def format_datetime(time) | |
time.strftime(@datetime_format || DATE_TIME_FORMAT) | |
end | |
def msg2str(msg) | |
case msg | |
when ::String | |
msg | |
when ::Exception | |
"#{ msg.classs } -- #{ msg.message }\n" << | |
(msg.backtrace || []).join("\n") | |
else | |
msg.inspect | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment