# Customize logger messages in Rails2?

**URL:** <https://www.ruby-forum.com/t/customize-logger-messages-in-rails2/153066>\
**Category:** Rails\
**Created:** [November 11, 2008, 7:47pm UTC](https://www.ruby-forum.com/t/customize-logger-messages-in-rails2/153066 "2008-11-11T19:47:17Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jrochkind](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/779978/32.png) [@jrochkind](https://www.ruby-forum.com/u/jrochkind)\
**Post date:** [November 11, 2008, 7:47pm UTC](https://www.ruby-forum.com/t/customize-logger-messages-in-rails2/153066/1 "2008-11-11T19:47:17Z")

</div>

In Rails2, what’s the best way to customize the format of logger output  
in Rails?

In Rails1, due to Rails weird monkey-patching of Logger, you couldn’t  
just subclass Logger and then set it as the logger to be used. Has this  
been improved in Rails2, is there a cleaner way to do this now?

In Rails1, the only good way I found to customize the logger output was  
to myself monkey-patch Logger at the end of environment.rb, over-riding  
format\_message:

class Logger  
def format\_message(severity, timestamp, progname, msg)  
# stuff to output messages just the way I want.  
end  
end

However, oddly, in Rails2 this seems to have stopped having any effect  
at all. The logger output is still uncustomized, Rails default.

Any advice? Thanks in advance to Frederick if he pays attention to this,  
because so far he’s the only one who both understands what I’m talking  
about and pays attention to me 🙂 I already owe you many beverages of  
your choice, Frederick.

---

<div class="post-metadata">

**Author:** ![jrochkind](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/779978/32.png) [@jrochkind](https://www.ruby-forum.com/u/jrochkind)\
**Post date:** [November 11, 2008, 10:05pm UTC](https://www.ruby-forum.com/t/customize-logger-messages-in-rails2/153066/2 "2008-11-11T22:05:04Z")

</div>

On 11 Nov 2008, at 18:47, Jonathan R. wrote:

> been improved in Rails2, is there a cleaner way to do this now?
> 
> In Rails1, the only good way I found to customize the logger output  
> was  
> to myself monkey-patch Logger at the end of environment.rb, over-  
> riding  
> format\_message:

Well if you look at the source to BufferedLogger in rails 2, the add  
method (which is what the rest of rails appears to be calling) just  
appends the message to the buffer. (or in other words looks like you  
should be overriding add on ActiveSupport::BufferedLogger or subclass  
that and set config.logger to an instance of your subclass)

Fred

---

<div class="post-metadata">

**Author:** ![jrochkind](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/779978/32.png) [@jrochkind](https://www.ruby-forum.com/u/jrochkind)\
**Post date:** [November 18, 2008, 1:18am UTC](https://www.ruby-forum.com/t/customize-logger-messages-in-rails2/153066/3 "2008-11-18T01:18:14Z")

</div>

Yeah, thanks. I found this patch, which adds formatter functionality to  
BufferedLogger:

[http://rails.lighthouseapp.com/projects/8994/tickets/1307-bufferedlogger-should-support-message-formatting](http://rails.lighthouseapp.com/projects/8994/tickets/1307-bufferedlogger-should-support-message-formatting)

Using that as a guide, I have created a sub-class of BufferedLogger that  
does what I want, until/unless that makes it into a future Rails  
release.

But now I’m stuck figuring out how to tell Rails to use my new sub-class  
(or an instantiation of it) of a BufferedLogger. Nothing I try to do  
seems to work. Setting RAILS\_DEFAULT\_LOGGER myself to my instantiated  
logger in environment.rb, no. Setting config.logger to my instantiated  
logger in environment.rb, no.

Anyone figured out how to actually tell Rails2 to use a new logger  
object?

(Odd that it’s taken so long to get logging right in Rails.)

Jonathan

Frederick C. wrote:

> On 11 Nov 2008, at 18:47, Jonathan R. wrote:
> 
> > been improved in Rails2, is there a cleaner way to do this now?
> > 
> > In Rails1, the only good way I found to customize the logger output  
> > was  
> > to myself monkey-patch Logger at the end of environment.rb, over-  
> > riding  
> > format\_message:
> 
> Well if you look at the source to BufferedLogger in rails 2, the add  
> method (which is what the rest of rails appears to be calling) just  
> appends the message to the buffer. (or in other words looks like you  
> should be overriding add on ActiveSupport::BufferedLogger or subclass  
> that and set config.logger to an instance of your subclass)
> 
> Fred

---

<div class="post-metadata">

**Author:** ![jrochkind](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/779978/32.png) [@jrochkind](https://www.ruby-forum.com/u/jrochkind)\
**Post date:** [November 18, 2008, 1:30am UTC](https://www.ruby-forum.com/t/customize-logger-messages-in-rails2/153066/4 "2008-11-18T01:30:08Z")

</div>

Okay, I finally figured out how to get this to work. Leave this here for  
future list archive searchers, since I had to put together a bunch of  
pieces.

My custom logger, based on this patch:  
[http://rails.lighthouseapp.com/projects/8994/tickets/1307-bufferedlogger-should-support-message-formatting](http://rails.lighthouseapp.com/projects/8994/tickets/1307-bufferedlogger-should-support-message-formatting)

is in my lib/umlaut\_logger.rb. So now that custom logger can take a  
formatter, like standard ruby Logger can.

For some reason, inside the environmental config block, auto-loading  
from constants doesn’t seem to work yet, I needed to require\_dependency  
it automatically. Then, I really wanted to take the existing log and  
severity level fields from RAILS\_DEFAULT\_LOGGER, but  
RAILS\_DEFAULT\_LOGGER isn’t set yet here. But if I wait until it IS set,  
then it’s too late for re-setting it to easily have an effect. So I had  
to reconstruct what it did from source, and other people’s suggetsions  
in the listserv archives.

So, in the config block:

```
require_dependency 'umlaut_logger'
severity_level = 

```

ActiveSupport::BufferedLogger.const\_get(config.log\_level.to\_s.upcase)  
log\_file = File.join(RAILS\_ROOT, “log”, “#{RAILS\_ENV}.log”)

```
our_logger = UmlautLogger.new(log_file, severity_level)
# eg, my real one is more complicated:
our_logger.formatter = lambda {|severity_label, message| 

```

“#{severity\_label}: #{message}”}

```
config.logger = our_logger

```

That was kind of a lot of work to get custom formatting. Phew.

---

<div class="post-metadata">

**Author:** ![jrochkind](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/779978/32.png) [@jrochkind](https://www.ruby-forum.com/u/jrochkind)\
**Post date:** [November 18, 2008, 1:29am UTC](https://www.ruby-forum.com/t/customize-logger-messages-in-rails2/153066/5 "2008-11-18T01:29:20Z")

</div>

On 18 Nov 2008, at 00:18, Jonathan R. wrote:

> release.
> 
> But now I’m stuck figuring out how to tell Rails to use my new sub-  
> class  
> (or an instantiation of it) of a BufferedLogger. Nothing I try to do  
> seems to work. Setting RAILS\_DEFAULT\_LOGGER myself to my instantiated  
> logger in environment.rb, no. Setting config.logger to my instantiated  
> logger in environment.rb, no.
> 
> Anyone figured out how to actually tell Rails2 to use a new logger  
> object?

I just set

config.logger = SyslogLogger.new ‘rails’  
config.logger.level = Logger::INFO

from production.rb and that works fine.

Fred

---

<div class="post-metadata">

**Author:** ![jrochkind](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/779978/32.png) [@jrochkind](https://www.ruby-forum.com/u/jrochkind)\
**Post date:** [November 18, 2008, 6:33pm UTC](https://www.ruby-forum.com/t/customize-logger-messages-in-rails2/153066/6 "2008-11-18T18:33:21Z")

</div>

I needed to blot out hugh binary data in the log so here’s what I use  
in 2.1. I have a hard time believing that there’s not a simpler way to  
blot out huge binary data but I’ve been unable to find it. I recall  
trying to use filter\_parameter\_logging from the controllers, but it  
didn’t work for me at the time (likely my issue, not Rails). Anyhow  
this code shows one way to muck with the logger, maybe it’ll help.  
It’s in my environment.rb. Bananas not included.

module ActiveSupport  
class BufferedLogger  
def add(severity, message = nil, progname = nil, &block)  
return if @level \> severity  
message = (message || (block && block.call) || progname).to\_s  
message.to\_str.gsub!(/(\s|()x’([0-9a-fA-F]\*)’(,|\s|))/,’\1x  
‘BINARY\_DATA’\3’)

```
  level = {
    0 => "D",
    1 => "I",
    2 => "W",
    3 => "E",
    4 => "F"
  }[severity] || "U"

  message = "[%s: %s #%d] %s" % [level,
                                 Time.now.strftime("%m%d %H:%M:

```

%S"),  
$$,  
message]

```
  message = "#{message}\n" unless message[-1] == ?\n
  buffer << message
  auto_flush
  message
end

```

end  
end

---

<div class="post-metadata">

**Author:** ![jrochkind](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/779978/32.png) [@jrochkind](https://www.ruby-forum.com/u/jrochkind)\
**Post date:** [November 18, 2008, 1:44am UTC](https://www.ruby-forum.com/t/customize-logger-messages-in-rails2/153066/7 "2008-11-18T01:44:44Z")

</div>

Argh, this still isn’t quite there yet. Now I can’t start my app with  
script/console, which is convenient sometimes. Because in  
script/console, somehow ActiveSupport hasn’t yet been loaded when it  
loads the environment.

So I had to add a “require ‘active\_support’” to my environment, and now  
console is happy. I hope that won’t mess up anything else in the Rails  
load order.

I had no idea I’d have to get so much into the guts of Rails just to set  
a custom log format.

Jonathan R. wrote:

> Okay, I finally figured out how to get this to work. Leave this here for  
> future list archive searchers, since I had to put together a bunch of  
> pieces.
> 
> My custom logger, based on this patch:  
> [#1307 BufferedLogger should support message formatting. - Ruby on Rails - rails](http://rails.lighthouseapp.com/projects/8994/tickets/1307-bufferedlogger-should-support-message-formatting)
> 
> is in my lib/umlaut\_logger.rb. So now that custom logger can take a  
> formatter, like standard ruby Logger can.
> 
> For some reason, inside the environmental config block, auto-loading  
> from constants doesn’t seem to work yet, I needed to require\_dependency  
> it automatically. Then, I really wanted to take the existing log and  
> severity level fields from RAILS\_DEFAULT\_LOGGER, but  
> RAILS\_DEFAULT\_LOGGER isn’t set yet here. But if I wait until it IS set,  
> then it’s too late for re-setting it to easily have an effect. So I had  
> to reconstruct what it did from source, and other people’s suggetsions  
> in the listserv archives.
> 
> So, in the config block:
> 
> ```
> require_dependency 'umlaut_logger'
> severity_level = 
> 
> ```
> 
> ActiveSupport::BufferedLogger.const\_get(config.log\_level.to\_s.upcase)  
> log\_file = File.join(RAILS\_ROOT, “log”, “#{RAILS\_ENV}.log”)
> 
> ```
> our_logger = UmlautLogger.new(log_file, severity_level)
> # eg, my real one is more complicated:
> our_logger.formatter = lambda {|severity_label, message| 
> 
> ```
> 
> “#{severity\_label}: #{message}”}
> 
> ```
> config.logger = our_logger
> 
> ```
> 
> That was kind of a lot of work to get custom formatting. Phew.
