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
|
|
|
|
|
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
|
|
|
attr_reader :buffer
|
|
|
|
|
|
|
|
def initialize(log, level = DEBUG)
|
|
|
|
@level = level
|
2007-10-03 19:43:12 -04:00
|
|
|
@buffer = []
|
|
|
|
@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-07-29 13:49:38 -04:00
|
|
|
@guard.synchronize do
|
|
|
|
buffer << message
|
|
|
|
end
|
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
|
2007-10-13 17:40:12 -04:00
|
|
|
class_eval <<-EOT, __FILE__, __LINE__
|
2007-09-25 12:41:46 -04:00
|
|
|
def #{severity.downcase}(message = nil, progname = nil, &block)
|
|
|
|
add(#{severity}, message, progname, &block)
|
2007-09-24 23:47:37 -04:00
|
|
|
end
|
2007-10-13 17:40:12 -04:00
|
|
|
|
2007-09-24 23:47:37 -04:00
|
|
|
def #{severity.downcase}?
|
2007-10-07 14:54:36 -04:00
|
|
|
#{severity} >= @level
|
2007-09-24 23:47:37 -04:00
|
|
|
end
|
|
|
|
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?
|
|
|
|
old_buffer = @buffer
|
|
|
|
@buffer = []
|
2008-07-29 14:01:25 -04:00
|
|
|
@log.write(old_buffer.join)
|
2008-01-13 15:39:51 -05:00
|
|
|
end
|
|
|
|
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
|
2007-09-24 23:47:37 -04:00
|
|
|
end
|
2007-10-03 19:43:12 -04:00
|
|
|
end
|