Created
September 10, 2011 21:24
-
-
Save anithri/1208809 to your computer and use it in GitHub Desktop.
rmu color formatter example
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
class ColorFormatter < Log4r::Formatter | |
attr_accessor :color, :label, :colors, :format_string | |
def colors | |
@level_colors || [:cyan, :green, :yellow, :light_red, :red] | |
end | |
def initialize(opts={}) | |
@depth = (opts[:depth] || 7 ).to_i | |
@color = opts[:color] || true | |
@format_string = opts[:format_string] || "%d [%9l] %m" | |
@trace_format_string = opts[:trace_format_string] || " $FROM %T" | |
@level_colors = opts[:level_colors] || | |
end | |
def format(event) | |
event_color = colors[event.level -1] | |
buff = @label ? sprintf(@format_string, Log4r::MaxLevelLength, | |
Log4r::LNAMES[event.level]) : "" | |
buff << format_object(event.data) | |
buff << (event.tracer.nil? ? "" : "#from: #{event.tracer[0]}") | |
buff << "\n" | |
self.color ? buff.colorize(event_color) : buff | |
end | |
def format_object(obj) | |
if obj.kind_of? Exception | |
return "Caught #{obj.class}: #{obj.message}\n\t" + obj.backtrace[0...@depth].join("\n\t") | |
elsif obj.kind_of? String | |
return obj | |
else # inspect the object | |
return "#{obj.class}: #{obj.inspect}" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment