Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

BufferedLogger should support message formatting.

#1307

Currently, ActiveSupport::BufferedLogger cannot do any message formatting. It would be very useful to see the severity of the message along with a timestamp for anything that is logged. This functionality is supported by the built-in Ruby Logger class, though only the time format is customizable.

I have created a patch allows you to specify your own formatting function to format the message in any way you wish. The default format remains the same - just the message itself. But you can do something like:

logger = ActiveSupport::BufferedLogger.new(STDOUT) logger.formatter = lambda { |severity_label, message| "[#{severity_label}] #{message}" } logger.info "test"

and get this in the log:

[INFO] test

You could add other things to the format, like a timestamp. The function is always called with 2 arguments, the first one being a string equal to the severity level, and the second one being the message itself. The severity levels are from the Severity module inside the BufferedLogger class - "DEBUG", "INFO", "WARN", etc.

I have also added 2 tests for this code.

Reported by Gary Gabriel · October 31st, 2008 @ 03:27 AM

State: stale
Milestone: 2.x
Assigned to: Yehuda Katz (wycats) Yehuda Katz (wycats)
Importance: Low

Activity

  1. jrochkind
    jrochkind

    This functionality would be much welcome, it seems odd that it was left out.

    I'm really not sure why BufferedLogger doesn't sub-class the standard Ruby Logger in the first place though, and allow formatting through the same way the standard Ruby Logger does, with a Logger::Formatter object?

    I guess this patch does use a Logger::Formatter type object, but I wonder why we're re-inventing the wheel instead of basing it on ruby Logger?

    November 17th, 2008 @ 11:39 PM

  2. Pratik
    Pratik
    • Milestone cleared.
    • Assigned user set to Pratik
    • Title changed from [PATCH] BufferedLogger should support message formatting. to BufferedLogger should support message formatting.

    January 19th, 2009 @ 08:09 PM

  3. Pratik
    Pratik
    • Milestone set to 2.x

    January 30th, 2009 @ 06:05 PM

  4. Pratik
    Pratik
    • Assigned user changed from Pratik to Yehuda Katz (wycats)

    January 30th, 2009 @ 06:26 PM

  5. Pratik
    Pratik
    • State changed from new to stale

    April 21st, 2009 @ 03:29 PM

  6. Tony Collen
    Tony Collen

    No love for this ticket? This would be incredibly useful right now.

    August 21st, 2009 @ 05:42 PM

  7. Deleted User
    Deleted User

    Could we change this from 'stale' to 'about time'? Why would any system include a logger with no formatting option?

    September 23rd, 2009 @ 07:41 PM

  8. Sebastian
    Sebastian
    • 1 Would be very useful!

    September 23rd, 2009 @ 09:22 PM

  9. mharper
    mharper

    Definitely would be useful.

    January 15th, 2010 @ 09:24 PM

  10. Jeff Kreeftmeijer
    Jeff Kreeftmeijer
    • Tag cleared.
    • Importance changed from to Low

    Automatic cleanup of spam.

    November 8th, 2010 @ 08:27 AM

  11. bingbing