Created
April 17, 2012 10:42
-
-
Save quark-zju/2405208 to your computer and use it in GitHub Desktop.
A colorful Logger for ruby
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 'term/ansicolor' | |
require 'logger' | |
class String | |
include Term::ANSIColor | |
end | |
class ColorfulLogger < Logger | |
def initialize(logdev, shift_age = 0, shift_size = 1048576) | |
super(logdev, shift_age, shift_size) | |
self.formatter = proc do |severity, datetime, progname, msg| | |
pid_colored = ('%s#%d' % [ progname, $$]).to_s.dark.send([:red,:green,:cyan,:magenta,:yellow][$$%5]) | |
severity_colored = ("%5s" % severity).bold.send({"DEBUG"=>:cyan, "INFO"=>:white, "WARN"=>:yellow, "ERROR"=>:red, "FATAL"=>:red}[severity.to_s] || :magenta) | |
"%s [%s] %s %s\n" % [datetime.strftime('%Y-%m-%d %H:%M:%S'), pid_colored, severity_colored, msg] | |
end | |
end | |
def write(msg) | |
self.info msg.chomp | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment