When you running your rails application via mongrel or lighttpd by using script/console command in development mode, you can see the terminal logging running in colorize mode, come to an idea to creating the same logging output for some of my background process. Do you know what is actually made up the color on the terminal? Basically it just special character code to color the terminal, and every terminal application have slight different implementation. Last time I was playing around with hyper terminal application and found this, then implement this on mac terminal.
I have make a simple method to do this for ruby programming language:
# usage:
# format_log("This is log message", {:background => "red", :foreground => "white", :special => "reset"})
#
def format_log(str, params)
specials = {
"reset" => 0,
"bold" => 1,
"half-bright" => 2,
"underscore" => 4,
"blink" => 5,
"reverse" => 7,
"normal-bright" => 22,
"no-underline" => 24,
"blink" => 25,
"no-reverse" => 27
}
foregrounds = {
"black" => 30,
"red" => 31,
"green" => 32,
"black" => 30,
"red" => 31,
"green" => 32,
"brown" => 33,
"blue" => 34,
"magenta" => 35,
"cyan" => 36,
"white" => 37
}
backgrounds = {
"black" => 40,
"red" => 41,
"green" => 42,
"brown" => 43,
"blue" => 44,
"magenta" => 45,
"cyan" => 46,
"white" => 47,
"default" => 49
}
puts "\033[#{backgrounds[params[:background]]};#{foregrounds[params[:foreground]]}m #{str} \033[#{specials[params[:special]]}m"
end
Sample below:
It tested on mac terminal, not sure whether is running on others. When free, I will make this tiny little helper to become a more mature plugin :-)