Skip to content

Instantly share code, notes, and snippets.

@billmers
Created February 16, 2013 00:54
Show Gist options
  • Save billmers/4964887 to your computer and use it in GitHub Desktop.
Save billmers/4964887 to your computer and use it in GitHub Desktop.
colorized logger #ruby #rails
module Logging
class ColorizedFormatter
SEVERITY_TO_ANSI_CODE ||= {'DEBUG'=>'0;37', 'INFO'=>'1;37', 'WARN'=>'1;33', 'ERROR'=>'0;31', 'FATAL'=>'1;31', 'UNKNOWN'=>'1;34'}
def call(severity, time, progname, msg)
return "#{msg}\n" if Rails.application.config.colorize_logging == false
reset_code = "\033[0m"
color_code = "\033[" + SEVERITY_TO_ANSI_CODE[severity] + "m"
msg.slice!(0..1) if msg.start_with? "\n\n"
"#{reset_code}#{color_code}#{msg.chomp}#{reset_code}\n"
end
end
end
require_relative 'colorized_formatter'
module Logging
class ColorizedLogger < ActiveSupport::BufferedLogger
def formatter=(formatter)
@log.formatter = formatter
end
def initialize(log, level = 0)
super
self.formatter = ColorizedFormatter.new
self.level = level
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment