views:

270

answers:

1

It appears that using ActiveRecord (which requires ActiveSupport) messes with the Logger class, resulting in difficulties. 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:

info
warn
error
fatal 

So I did some searching about and after looking at activesupport:

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. Does anyone know if there is an easier way to get the desired result?

Thanks!

+1  A: 

The same Problem bugged me the last Hour, but the Solution is quite simple.

ActiveSupport uses the SimpleFormatter, you have to set the 'old' Formatter by hand.

require 'rubygems'
require 'active_support'
require 'logger'

log = Logger.new(STDERR)
log.sev_threshold = Logger::INFO
log.datetime_format = "%Y-%m-%d %H:%M:%S"
log.formatter = Logger::Formatter.new

Ps: Sry for my bad English :)

Markus Burger