Is log4r a good option for proper logging in rails app (date time, severity, notification etc?) or is there something else out there?
Asked
Active
Viewed 3,318 times
1 Answers
1
Put this into a ruby file , put the file the /lib folder (Convention) and 'require' it from your environment.
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("%d-%m-%Y %H:%M:%S"),
message] unless message[-1] == ?\n
buffer << message
auto_flush
message
end
end
And these lines in your environment inside the initializer block.
config.log_level = ENV['RAILS_ENV']=='development' ?
ActiveSupport::BufferedLogger::Severity::INFO :
ActiveSupport::BufferedLogger::Severity::DEBUG

Shreyas
- 8,737
- 7
- 44
- 56
-
So how do I log stuff? Rails.logger.debug? – badnaam Sep 07 '10 at 22:16
-
Yeah. But this time when you do a logger debug you get all the detail you wanted. Just make sure you set the environment right in config.log_level – Shreyas Sep 08 '10 at 03:17
-
Shreyas, How would your code change if you wanted to insert the errors into a table? Perhaps assume a model named Log. – Mutuelinvestor Apr 08 '13 at 17:01