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}/log/cron.log")
CRON_LOG.level = Logger::INFO
And then, when I want to log something, just do that:
CRON_LOG.info "something"
It works fine, but I'd like to add the current timestamp before each log message. Of course I can just add Time.now
to my log message, but I'd like to know if there is a way to add it as default to every log message. How can I do that ?
Thanks.
The easiest way to make a SysLog-formatted logger is to assign a formatter directly:
You can redefine the proper method (e.g. adding the following to
environment.rb
or in an initializer):[Caution: this could disrupt other loggers; see Stephen's answer for a safe solution - jph]