Custom logger in Rails 14
The default logging format in Rails lacks timestamp and log level, this is how configure logging to the format of your preference.
In Rail 1.2.X, it was enough to just redefine the format_message of the Logger class by just inserting the following lines at the bottom of config/environment.rb:
# custom logging format
class Logger
private
def format_message(severity, timestamp, progname, msg)
"[%5s %s] %s\n" % [severity,
timestamp.strftime("%m/%d %H:%M:%S"),
msg]
end
end
No longer in Rails 2.X as the logger object is now an instance of the ActiveSupport::BufferedLogger class. A bit of monkey patching is needed. Create a new file called custom_logger.rb in the lib directory:
require 'active_support'
# Logger class for custom logging format
class CustomLogger < ActiveSupport::BufferedLogger
private
# CustomLogger doesn't define strings for log levels
# so we have to do it ourselves
def severity_string(level)
case level
when DEBUG
:DEBUG
when INFO
:INFO
when WARN
:WARN
when ERROR
:ERROR
when FATAL
:FATAL
else
:UNKNOWN
end
end
public
# monkey patch the CustomLogger add method so that
# we can format the log messages the way we want
def add(severity, message = nil, progname = nil, &block)
return if @level > severity
message = (message || (block && block.call) || progname).to_s
# If a newline is necessary then create a new message ending with a newline.
# Ensures that the original message is not mutated.
message = "[%5s %s] %s\n" % [severity_string(severity),
Time.now.strftime("%m/%d %H:%M:%S"),
message] unless message[-1] == ?\n
buffer << message
auto_flush
message
end
end
And modify environment.rb the following way:
require File.join(File.dirname(__FILE__), 'boot')
require 'custom_logger'
Rails::Initializer.run do |config|
...
# Force all environments to use the same logger level
# (by default production uses :info, the others :debug)
config.log_level = ENV['RAILS_ENV']=='production' ?
ActiveSupport::BufferedLogger::Severity::INFO :
ActiveSupport::BufferedLogger::Severity::DEBUG
# initializing custom logger
config.logger = CustomLogger.new(config.log_path, config.log_level)
end
That does 2 things:
- if running in production mode, no message with a level lower than INFO will be printed
- the format of every log message will include level, timestamp and message
Example:
[ INFO 08/04 20:34:41] Session ID: 78e4de86b30d6d704a9f6f44717a4288
[ INFO 08/04 20:34:41] Parameters: {"action"=>"buy", "controller"=>"article"}

Handy heads up. Thanks.
Actually, after tinkering and thinking about it more, I'm not sure this is the right approach. I think the removal of message formatting code from the preferred logging class was smart. You are free to wrap and massage your messages before they hit that level of code, and I think that's what I'm going to stick with.. maybe.. ;)
I'm wondering if there is a way to get around this by not having to create the custom_logger.rb... is the monkey patching really necessary?
Actually, you have a problem here: if the message ends in \n, you won't be adding your formatting, because of the unless at the end.
Try the below instead. It also guarantees that the string that's returned is the same as the one that was passed in, which is the behavior of the unpatched logger
Oops. Extra \n in the format
Very very useful. I got that extra n out of the format. I guess everything works fine apart from it.
The default 2.3.5 Rails logger does not print a Timestamp, or other useful background information.
It seems so wonderful,and the interface is clean,but it can't print a timestamp.
I differ with most people here; I found this blog post I couldn't stop until I finished, even though it wasn't just what I had been searching for, was still a nice read though. I will instantly get your blog feed to stay in touch of any updates. Rent a car
Have you ever wanted to make such a difference in a person's life? Now is the time for you to act. You can change a life for the better and you can save a life today. Read this article and find out how. Chicago Brain Injury Lawyer
It is so amazing that you’ve got so many people to help you compose the blog and it is really exciting to read and enjoy all the details of your stories that have never touched me. bulk sunglasses
I have symptoms like inability to achieve an erection, followed by anxiety and depression on a regular basis, but now I am thinking that I may need viagra. wholesale sunglasses distributor
Hi, I am using the Custom Logger as mentioned in this post. What do I need to do if I need to roll the Rails log files every day? Currently, the production.log file is very big, so if I can create a different file for each day, it would be easy to manage the logs.