July 12, 2007
Older: Clogging: Code Blogging
Newer: Last Modified File in Directory
Customizing Logger
# put this in your lib folder as logger_format.rb and
# require it in your environment or an initializer
class Logger
def format_message(level, time, progname, msg)
"#{time.to_s(:long)} -- #{msg}\n"
end
end
Then you will have prettier log formatting like this:
Feel free to tweak the format_message method to your heart’s content.
Update A Day Later
topfunky made a good point that I hadn’t thought of. As such, I now use his hodel 3000 compliant logger. It’s easy to install and works like a charm with several log analyzer’s. Give it a try.
4 Comments
Jul 13, 2007
one of the coolest uses of this is to add the PID of the executing process to the logger output:
“#{msg} (pid:#{$$})”
then you can use Chris Hobbs’ rawk.rb to analyze yours logs with pretty output.
Jul 13, 2007
Sweet tip… thanks! I live by my logs.
Jul 13, 2007
I tried rawk.rb but ended up writing a logger that can be parsed by Eric Hodel’s pl_analyze script. I use it on every one of my sites.
http://nubyonrails.com/articles/a-hodel-3000-compliant-logger-for-the-rest-of-us
Also, you should be careful when opening the Logger class. Subclass it and customize that when you’re writing your own scripts or you can cause havoc in other libraries that expect the default methods, arguments and behavior of the Logger class.
Jul 13, 2007
@oliver – nice.
@topfunky – Thanks for the info. I’m updating the post to point people to your logger and I’m trying it out as well.
Sorry, comments are closed for this article to ease the burden of pruning spam.