Add current time before log message

后端 未结 2 1555
悲&欢浪女
悲&欢浪女 2021-02-07 07:32

I created a custom logger for my application, called CRON_LOG, just by adding this piece of code to config/environment.rb

CRON_LOG = Logger.new(\"#{Rails.root}/l         


        
相关标签:
2条回答
  • 2021-02-07 07:42

    You can redefine the proper method (e.g. adding the following to environment.rb or in an initializer):

    class Logger
      def format_message(severity, timestamp, progname, msg)
        "#{timestamp} (#{$$}) #{msg}\n"
      end
    end
    

    [Caution: this could disrupt other loggers; see Stephen's answer for a safe solution - jph]

    0 讨论(0)
  • 2021-02-07 07:53

    The easiest way to make a SysLog-formatted logger is to assign a formatter directly:

    logger = Logger.new Rails.root.join('path', 'to', 'log')
    logger.formatter = Logger::Formatter.new
    logger.info 'Hello, world!'
    # Writes:
    #
    #   I, [2011-06-02T20:02:34.579501 #15444]  INFO -- : Hello, world!
    
    0 讨论(0)
提交回复
热议问题