Created
February 16, 2013 00:54
-
-
Save billmers/4964887 to your computer and use it in GitHub Desktop.
colorized logger #ruby #rails
This file contains hidden or 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
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 |
This file contains hidden or 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_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