public
Last active

A colorful Logger for ruby

  • Download Gist
colorful_logger.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.