Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
641 views
in Technique[技术] by (71.8m points)

logging - Rails logger format string configuration

How can I configure the rails logger to output its log strings in another format? I would like to get something that is more informative like:

[Log Level] [Time] [Message]

Debug : 01-20-2008 13:11:03.00 : Method Called

This would really help me when I want to tail my development.log for messages that only come from a certain log level, like debug.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Did some digging and found this post in the RubyOnRails Talk google group.

So I modified it a little bit and put it at the end of my environment.rb:

module ActiveSupport
  class BufferedLogger
    def add(severity, message = nil, progname = nil, &block)
      return if @level > severity
      message = (message || (block && block.call) || progname).to_s

      level = {
        0 => "DEBUG",
        1 => "INFO",
        2 => "WARN",
        3 => "ERROR",
        4 => "FATAL"
      }[severity] || "U"

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

      message = "#{message}
" unless message[-1] == ?

      buffer << message
      auto_flush
      message
    end
  end
end

This results in a format string like this:

[DEBUG: 0121 10:35:26 #57078] Rendered layouts/_header (0.00089)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...