Created
July 13, 2010 16:31
-
-
Save gilles/474132 to your computer and use it in GitHub Desktop.
rails line logger
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#inspired (a lot) by http://blog.gugl.org/archives/47 | |
# Note, to achieve this I should only have to define a formatter. | |
# why BufferedLogger instead of Logger with a buffered IO? | |
# I suspect there is a reason for BufferedLogger so I'll keep it | |
class LineLogger < ActiveSupport::BufferedLogger | |
SEVERITIES = Severity.constants.inject([]) {|arr,c| arr[Severity.const_get(c)] = c; arr} | |
attr_writer :formatter | |
def initialize(log, level = DEBUG, formatter = nil) | |
super(log, level) | |
@formatter = formatter || Logger::Formatter.new | |
end | |
def add(severity, message = nil, progname = nil, & block) | |
return if @level > severity | |
message = @formatter.call(SEVERITIES[severity], Time.now, progname, message) | |
buffer << message | |
auto_flush | |
message | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
begin | |
config.logger = LineLogger.new(paths.log.paths[0]) | |
config.logger.level = LineLogger::WARN | |
# This is what Rails does by default in production mode when using the default logger | |
config.logger.auto_flushing = false | |
rescue StandardError | |
#this is also what rails does in case of failure, see initializer.rb | |
config.logger = ActiveSupport::BufferedLogger.new(STDERR) | |
config.logger.level = ActiveSupport::BufferedLogger::WARN | |
config.logger.warn( | |
"Rails Error: Unable to access log file. Please ensure that #{paths.log.paths[0]} exists and is chmod 0666. " + | |
"The log level has been raised to WARN and the output directed to STDERR until the problem is fixed." | |
) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment