July 12, 2007

Posted by John

Tagged clogging and logger

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:

Custom Formatted Logger

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

  1. 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.

  2. Sweet tip… thanks! I live by my logs.

  3. 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.

  4. @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.

About

Authored by John Nunemaker (Noo-neh-maker), a programmer who has fallen deeply in love with Ruby. Learn More.

Projects

Flipper
Release your software more often with fewer problems.
Flip your features.