2011-03-11 16:29:39 -05:00
|
|
|
require 'thread'
|
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
|
2011-10-21 13:43:52 -04:00
|
|
|
old_logger_level = @tmp_levels[Thread.current]
|
2007-09-24 23:47:37 -04:00
|
|
|
begin
|
2011-10-21 13:43:52 -04:00
|
|
|
@tmp_levels[Thread.current] = temporary_level
|
2007-09-24 23:47:37 -04:00
|
|
|
yield self
|
|
|
|
ensure
|
2011-10-21 13:43:52 -04:00
|
|
|
if old_logger_level
|
|
|
|
@tmp_levels[Thread.current] = old_logger_level
|
|
|
|
else
|
|
|
|
@tmp_levels.delete(Thread.current)
|
|
|
|
end
|
2007-09-24 23:47:37 -04:00
|
|
|
end
|
|
|
|
else
|
|
|
|
yield self
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-10-21 13:43:52 -04:00
|
|
|
attr_writer :level
|
2007-10-03 19:43:12 -04:00
|
|
|
attr_reader :auto_flushing
|
2007-09-24 23:47:37 -04:00
|
|
|
|
|
|
|
def initialize(log, level = DEBUG)
|
|
|
|
@level = level
|
2011-10-21 13:43:52 -04:00
|
|
|
@tmp_levels = {}
|
2011-04-26 13:41:59 -04:00
|
|
|
@buffer = Hash.new { |h,k| h[k] = [] }
|
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)
|
2011-05-18 06:43:29 -04:00
|
|
|
@log = open_log(log, (File::WRONLY | File::APPEND))
|
2007-09-24 23:47:37 -04:00
|
|
|
else
|
2008-03-12 21:54:34 -04:00
|
|
|
FileUtils.mkdir_p(File.dirname(log))
|
2011-05-18 06:43:29 -04:00
|
|
|
@log = open_log(log, (File::WRONLY | File::APPEND | File::CREAT))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def open_log(log, mode)
|
2011-05-21 01:27:35 -04:00
|
|
|
open(log, mode).tap do |open_log|
|
|
|
|
open_log.set_encoding(Encoding::BINARY) if open_log.respond_to?(:set_encoding)
|
|
|
|
open_log.sync = true
|
2007-09-24 23:47:37 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-10-21 13:43:52 -04:00
|
|
|
def level
|
|
|
|
@tmp_levels[Thread.current] || @level
|
|
|
|
end
|
|
|
|
|
2007-09-25 12:41:46 -04:00
|
|
|
def add(severity, message = nil, progname = nil, &block)
|
2011-10-21 13:43:52 -04:00
|
|
|
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
|
|
|
|
|
2009-07-25 11:03:58 -04:00
|
|
|
# Dynamically add methods such as:
|
|
|
|
# def info
|
|
|
|
# def warn
|
|
|
|
# def debug
|
2011-05-24 00:49:05 -04:00
|
|
|
Severity.constants.each do |severity|
|
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?
|
2011-10-21 13:43:52 -04:00
|
|
|
#{severity} >= level # DEBUG >= @level
|
2008-12-28 05:21:10 -05:00
|
|
|
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
|
2011-10-21 13:43:52 -04:00
|
|
|
write_buffer(buffer)
|
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
|
2011-10-21 13:43:52 -04:00
|
|
|
|
|
|
|
# Clear buffers associated with dead threads or else spawned threads
|
|
|
|
# that don't call flush will result in a memory leak.
|
|
|
|
flush_dead_buffers
|
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
|
2011-04-26 13:41:59 -04:00
|
|
|
@buffer[Thread.current]
|
2008-08-19 00:33:46 -04:00
|
|
|
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
|
2011-10-21 13:43:52 -04:00
|
|
|
|
|
|
|
# Find buffers created by threads that are no longer alive and flush them to the log
|
|
|
|
# in order to prevent memory leaks from spawned threads.
|
|
|
|
def flush_dead_buffers #:nodoc:
|
|
|
|
@buffer.keys.reject{|thread| thread.alive?}.each do |thread|
|
|
|
|
buffer = @buffer[thread]
|
|
|
|
write_buffer(buffer)
|
|
|
|
@buffer.delete(thread)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def write_buffer(buffer)
|
|
|
|
buffer.each do |content|
|
|
|
|
@log.write(content)
|
|
|
|
end
|
|
|
|
end
|
2007-09-24 23:47:37 -04:00
|
|
|
end
|
2007-10-03 19:43:12 -04:00
|
|
|
end
|