# ActiveRecord interferring with Logger

**URL:** <https://www.ruby-forum.com/t/activerecord-interferring-with-logger/174932>\
**Category:** Rails\
**Created:** [September 4, 2009, 10:19am UTC](https://www.ruby-forum.com/t/activerecord-interferring-with-logger/174932 "2009-09-04T10:19:45Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![rohitunderhill](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ecc23a/32.png) [@rohitunderhill](https://www.ruby-forum.com/u/rohitunderhill)\
**Post date:** [September 4, 2009, 10:19am UTC](https://www.ruby-forum.com/t/activerecord-interferring-with-logger/174932/1 "2009-09-04T10:19:45Z")

</div>

It appears that using ActiveRecord (which requires ActiveSupport)  
messes with the Logger class, resulting in difficulties in a non-Rails  
ActiveRecord-using project. This can be seen with some example code:

require ‘rubygems’  
#require ‘activerecord’  
require ‘logger’

log = Logger.new(STDERR)  
log.sev\_threshold = Logger::INFO  
log.datetime\_format = “%Y-%m-%d %H:%M:%S”

log.debug “debug”  
log.info “info”  
log.warn “warn”  
log.error “error”  
log.fatal “fatal”

Running this code will produce this lovely output:

I, [2009-09-02 10:49:39#27562] INFO – : info  
W, [2009-09-02 10:49:39#27562] WARN – : warn  
E, [2009-09-02 10:49:39#27562] ERROR – : error  
F, [2009-09-02 10:49:39#27562] FATAL – : fatal

However, if I uncomment the require ‘activerecord’ line, I instead get  
this less beautiful output:

info  
warn  
error  
fatal

So I did some searching about and after looking at activesupport’s  
logger.rb

> <https://github.com/rails/rails/blob/669fd84910586d4c791b6f5bf4320f68ac7845aa/activesupport/lib/active_support/core_ext/logger.rb>

I found the following “working solution”

log = Logger.new(STDERR)  
log.sev\_threshold = Logger::INFO  
log.datetime\_format = “%Y-%m-%d %H:%M:%S”  
class Formatter  
Format = “%s, [%s#%d] %5s – %s: %s\n”

```
  attr_accessor :datetime_format

  def initialize
    @datetime_format = nil
  end

  def call(severity, time, progname, msg)
    Format % [severity[0..0], format_datetime(time), $$, severity,

```

progname, msg2str(msg)]  
end

```
  private
    def format_datetime(time)
      if @datetime_format.nil?
         time.strftime("%Y-%m-%dT%H:%M:%S.") << "%06d " %

```

time.usec  
else  
time.strftime(@datetime\_format)  
end  
end

```
    def msg2str(msg)
      case msg
      when ::String
        msg
      when ::Exception
        "#{ msg.message } (#{ msg.class })\n" <<
        (msg.backtrace || []).join("\n")
      else
        msg.inspect
      end
    end

```

end  
f=Formatter.new  
f.datetime\_format = “%Y-%m-%d %H:%M:%S”  
log.formatter=f

Using the above, I get the output that I like. However, that seems to  
me gross and unRubylike, probably not even guarranteed to continue  
working when I upgrade to a newer version of ActiveRecord. Does anyone  
know if there is an easier and smarter way to get the desired result?

Many Thanks!

Rohit
