Last active
August 22, 2018 10:55
-
-
Save vladfaust/0689b06a615834fb0ead01c7a14c029b 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" | |
require "colorize" | |
# A custom log formatter. | |
# | |
# ``` | |
# require "./custom_log_formatter" | |
# logger = Logger.new(STDOUT, Logger::DEBUG, custom_log_formatter) | |
# logger.debug("Hello world!") | |
# DEBUG [12:45:52.520] Hello world! | |
# ``` | |
def custom_log_formatter | |
Logger::Formatter.new do |severity, time, progname, message, io| | |
fore, back = case severity | |
when Logger::DEBUG then {:dark_gray, nil} | |
when Logger::INFO then {:light_blue, nil} | |
when Logger::WARN then {:yellow, nil} | |
when Logger::ERROR then {nil, :red} | |
when Logger::FATAL then {nil, :red} | |
else {nil, nil} | |
end | |
prefix = (severity.to_s.rjust(5) + " [" + time.to_s("%X.%L") + ']').colorize | |
prefix.fore(fore) if fore | |
prefix.back(back) if back | |
io << prefix << ' ' << message | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment