Skip to content

Instantly share code, notes, and snippets.

@copiousfreetime
Created June 4, 2020 16:50
Show Gist options
  • Save copiousfreetime/15ee67dafeaed2151af23b45f1af6c7c to your computer and use it in GitHub Desktop.
Save copiousfreetime/15ee67dafeaed2151af23b45f1af6c7c to your computer and use it in GitHub Desktop.
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