Customizing Logger

July 12th, 2007

# 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 Responses to “Customizing Logger”

  1. Oliver Nicholas Says:

    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. Ben Says:

    Sweet tip… thanks! I live by my logs.

  3. topfunky Says:

    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. John Nunemaker Says:

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

Leave a Reply


(textile enabled)