2009-03-29 02:37:32 -04:00
|
|
|
require 'active_support/core_ext/class/attribute_accessors'
|
|
|
|
|
2007-09-24 23:47:37 -04:00
|
|
|
module ActiveSupport
|
|
|
|
# Inspired by the buffered logger idea by Ezra
|
|
|
|
class BufferedLogger
|
|
|
|
module Severity
|
|
|
|
DEBUG = 0
|
|
|
|
INFO = 1
|
|
|
|
WARN = 2
|
|
|
|
ERROR = 3
|
|
|
|
FATAL = 4
|
|
|
|
UNKNOWN = 5
|
|
|
|
end
|
|
|
|
include Severity
|
|
|
|
|
2007-10-04 15:52:10 -04:00
|
|
|
MAX_BUFFER_SIZE = 1000
|
|
|
|
|
2008-12-06 21:27:53 -05:00
|
|
|
##
|
|
|
|
# :singleton-method:
|
2007-09-24 23:47:37 -04:00
|
|
|
# Set to false to disable the silencer
|
|
|
|
cattr_accessor :silencer
|
|
|
|
self.silencer = true
|
|
|
|
|
|
|
|
# Silences the logger for the duration of the block.
|
|
|
|
def silence(temporary_level = ERROR)
|
|
|
|
if silencer
|
|
|
|
begin
|
|
|
|
old_logger_level, self.level = level, temporary_level
|
|
|
|
yield self
|
|
|
|
ensure
|
|
|
|
self.level = old_logger_level
|
|
|
|
end
|
|
|
|
else
|
|
|
|
yield self
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-10-03 19:43:12 -04:00
|
|
|
attr_accessor :level
|
|
|
|
attr_reader :auto_flushing
|
2007-09-24 23:47:37 -04:00
|
|
|
|
|
|
|
def initialize(log, level = DEBUG)
|
|
|
|
@level = level
|
2008-08-19 00:33:46 -04:00
|
|
|
@buffer = {}
|
2007-10-03 19:43:12 -04:00
|
|
|
@auto_flushing = 1
|
2008-07-29 13:49:38 -04:00
|
|
|
@guard = Mutex.new
|
2007-09-24 23:47:37 -04:00
|
|
|
|
|
|
|
if log.respond_to?(:write)
|
|
|
|
@log = log
|
|
|
|
elsif File.exist?(log)
|
|
|
|
@log = open(log, (File::WRONLY | File::APPEND))
|
|
|
|
@log.sync = true
|
|
|
|
else
|
2008-03-12 21:54:34 -04:00
|
|
|
FileUtils.mkdir_p(File.dirname(log))
|
2007-09-24 23:47:37 -04:00
|
|
|
@log = open(log, (File::WRONLY | File::APPEND | File::CREAT))
|
|
|
|
@log.sync = true
|
|
|
|
@log.write("# Logfile created on %s" % [Time.now.to_s])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-09-25 12:41:46 -04:00
|
|
|
def add(severity, message = nil, progname = nil, &block)
|
|
|
|
return if @level > severity
|
2007-09-28 10:09:35 -04:00
|
|
|
message = (message || (block && block.call) || progname).to_s
|
2007-09-27 02:16:20 -04:00
|
|
|
# If a newline is necessary then create a new message ending with a newline.
|
2007-09-27 00:50:58 -04:00
|
|
|
# Ensures that the original message is not mutated.
|
|
|
|
message = "#{message}\n" unless message[-1] == ?\n
|
2008-08-19 00:33:46 -04:00
|
|
|
buffer << message
|
2007-10-04 15:52:10 -04:00
|
|
|
auto_flush
|
2007-09-25 12:41:46 -04:00
|
|
|
message
|
|
|
|
end
|
|
|
|
|
2007-09-24 23:47:37 -04:00
|
|
|
for severity in Severity.constants
|
2008-12-28 05:21:10 -05:00
|
|
|
class_eval <<-EOT, __FILE__, __LINE__ + 1
|
|
|
|
def #{severity.downcase}(message = nil, progname = nil, &block) # def debug(message = nil, progname = nil, &block)
|
|
|
|
add(#{severity}, message, progname, &block) # add(DEBUG, message, progname, &block)
|
|
|
|
end # end
|
2007-10-13 17:40:12 -04:00
|
|
|
|
2008-12-28 05:21:10 -05:00
|
|
|
def #{severity.downcase}? # def debug?
|
|
|
|
#{severity} >= @level # DEBUG >= @level
|
|
|
|
end # end
|
2007-09-24 23:47:37 -04:00
|
|
|
EOT
|
|
|
|
end
|
|
|
|
|
2007-10-03 19:43:12 -04:00
|
|
|
# Set the auto-flush period. Set to true to flush after every log message,
|
|
|
|
# to an integer to flush every N messages, or to false, nil, or zero to
|
|
|
|
# never auto-flush. If you turn auto-flushing off, be sure to regularly
|
|
|
|
# flush the log yourself -- it will eat up memory until you do.
|
|
|
|
def auto_flushing=(period)
|
2007-10-04 15:52:10 -04:00
|
|
|
@auto_flushing =
|
|
|
|
case period
|
|
|
|
when true; 1
|
|
|
|
when false, nil, 0; MAX_BUFFER_SIZE
|
|
|
|
when Integer; period
|
|
|
|
else raise ArgumentError, "Unrecognized auto_flushing period: #{period.inspect}"
|
|
|
|
end
|
2007-10-03 19:43:12 -04:00
|
|
|
end
|
|
|
|
|
2007-09-24 23:47:37 -04:00
|
|
|
def flush
|
2008-07-29 13:49:38 -04:00
|
|
|
@guard.synchronize do
|
|
|
|
unless buffer.empty?
|
2008-08-19 00:33:46 -04:00
|
|
|
old_buffer = buffer
|
2008-07-29 14:01:25 -04:00
|
|
|
@log.write(old_buffer.join)
|
2008-01-13 15:39:51 -05:00
|
|
|
end
|
2008-12-10 17:36:58 -05:00
|
|
|
|
|
|
|
# Important to do this even if buffer was empty or else @buffer will
|
|
|
|
# accumulate empty arrays for each request where nothing was logged.
|
|
|
|
clear_buffer
|
2008-01-13 15:39:51 -05:00
|
|
|
end
|
2007-09-24 23:47:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def close
|
|
|
|
flush
|
|
|
|
@log.close if @log.respond_to?(:close)
|
|
|
|
@log = nil
|
|
|
|
end
|
2007-10-03 19:43:12 -04:00
|
|
|
|
|
|
|
protected
|
|
|
|
def auto_flush
|
2008-01-13 15:39:51 -05:00
|
|
|
flush if buffer.size >= @auto_flushing
|
2007-10-03 19:43:12 -04:00
|
|
|
end
|
2008-08-19 00:33:46 -04:00
|
|
|
|
|
|
|
def buffer
|
|
|
|
@buffer[Thread.current] ||= []
|
|
|
|
end
|
|
|
|
|
|
|
|
def clear_buffer
|
2008-09-18 09:51:19 -04:00
|
|
|
@buffer.delete(Thread.current)
|
2008-08-19 00:33:46 -04:00
|
|
|
end
|
2007-09-24 23:47:37 -04:00
|
|
|
end
|
2007-10-03 19:43:12 -04:00
|
|
|
end
|