Skip to content

Instantly share code, notes, and snippets.

@quark-zju
Created April 17, 2012 10:42
Show Gist options
  • Save quark-zju/2405208 to your computer and use it in GitHub Desktop.
Save quark-zju/2405208 to your computer and use it in GitHub Desktop.
A colorful Logger for ruby
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