I have the fo开发者_Go百科llowing logger class:
module AppLogger
  @@_loggers = {}
  def self.logger; self._creater_logger("my_app_name"); end
  def self._creater_logger(name)
    @@_loggers[name] ||= $0 == "irb" ? Logger.new(STDOUT) : Logger.new(Rails.root.join("log",Rails.env + ".#{name}.log"))
  end
  def self.format_exception(e)
    "#{e.message} [[\n\t\t#{e.backtrace.split("\n").map { |l| "\t\t#{l}" }.join("\n")}]]"
  end
end
and to use it:
AppLogger.logger.info(msg)
Is there a way to automatically add the timestamp to each log row (without manually adding it to each call to the logger)?
You could redefine a singleton of your class and add all or some levels of logging levels.
%w(debug info warn error fatal).each do |level|
  eval <<-end_eval
    def Applogger.logger.#{level}(message="", &blk)
      if message == "" and block_given?
        message = blk.call
      end
      super(<you time stamp>) { message }
    end
  end_eval
end
Basically add what ever your heart desires in the super method.
I grabbed this code from an old blog post of mine where I had to add custom info to my logger you could refer to that for more details.
http://www.snitchmedia.com/blog/2009/11/24/pimping-ruby-logger-instance/
 
         
                                         
                                         
                                         
                                        ![Interactive visualization of a graph in python [closed]](https://www.devze.com/res/2023/04-10/09/92d32fe8c0d22fb96bd6f6e8b7d1f457.gif) 
                                         
                                         
                                         
                                         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论