2011-03-11 16:29:39 -05:00
|
|
|
require 'thread'
|
2011-12-09 19:03:18 -05:00
|
|
|
require 'logger'
|
2011-12-13 04:36:32 -05:00
|
|
|
require 'active_support/core_ext/logger'
|
2009-03-29 02:37:32 -04:00
|
|
|
require 'active_support/core_ext/class/attribute_accessors'
|
2011-12-09 19:03:18 -05:00
|
|
|
require 'active_support/deprecation'
|
|
|
|
require 'fileutils'
|
2009-03-29 02:37:32 -04:00
|
|
|
|
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
|
2011-12-09 19:03:18 -05:00
|
|
|
logger = self.class.new @log_dest, temporary_level
|
|
|
|
yield logger
|
2007-09-24 23:47:37 -04:00
|
|
|
ensure
|
2011-12-09 19:03:18 -05:00
|
|
|
logger.close
|
2007-09-24 23:47:37 -04:00
|
|
|
end
|
|
|
|
else
|
|
|
|
yield self
|
|
|
|
end
|
|
|
|
end
|
2011-12-09 19:03:18 -05:00
|
|
|
deprecate :silence
|
2007-09-24 23:47:37 -04:00
|
|
|
|
2007-10-03 19:43:12 -04:00
|
|
|
attr_reader :auto_flushing
|
2011-12-09 19:03:18 -05:00
|
|
|
deprecate :auto_flushing
|
2007-09-24 23:47:37 -04:00
|
|
|
|
|
|
|
def initialize(log, level = DEBUG)
|
|
|
|
@level = level
|
2011-12-09 19:03:18 -05:00
|
|
|
@log_dest = log
|
|
|
|
|
|
|
|
unless log.respond_to?(:write)
|
|
|
|
unless File.exist?(File.dirname(log))
|
|
|
|
ActiveSupport::Deprecation.warn(<<-eowarn)
|
|
|
|
Automatic directory creation for '#{log}' is deprecated. Please make sure the directory for your log file exists before creating the logger.
|
|
|
|
eowarn
|
|
|
|
FileUtils.mkdir_p(File.dirname(log))
|
|
|
|
end
|
2011-05-18 06:43:29 -04:00
|
|
|
end
|
2011-12-09 19:03:18 -05:00
|
|
|
|
|
|
|
@log = open_logfile log
|
2011-05-18 06:43:29 -04:00
|
|
|
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-12-09 19:03:18 -05:00
|
|
|
deprecate :open_log
|
2007-09-24 23:47:37 -04:00
|
|
|
|
2011-10-21 13:43:52 -04:00
|
|
|
def level
|
2011-12-09 19:03:18 -05:00
|
|
|
@log.level
|
|
|
|
end
|
|
|
|
|
|
|
|
def level=(l)
|
|
|
|
@log.level = l
|
2011-10-21 13:43:52 -04:00
|
|
|
end
|
|
|
|
|
2007-09-25 12:41:46 -04:00
|
|
|
def add(severity, message = nil, progname = nil, &block)
|
2011-12-09 19:03:18 -05:00
|
|
|
@log.add(severity, message, progname, &block)
|
2007-09-25 12:41:46 -04:00
|
|
|
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)
|
|
|
|
end
|
2011-12-09 19:03:18 -05:00
|
|
|
deprecate :auto_flushing=
|
2007-10-03 19:43:12 -04:00
|
|
|
|
2007-09-24 23:47:37 -04:00
|
|
|
def flush
|
|
|
|
end
|
2011-12-09 19:03:18 -05:00
|
|
|
deprecate :flush
|
2007-09-24 23:47:37 -04:00
|
|
|
|
2011-12-10 19:31:59 -05:00
|
|
|
def respond_to?(method, include_private = false)
|
|
|
|
return false if method.to_s == "flush"
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2007-09-24 23:47:37 -04:00
|
|
|
def close
|
2011-12-09 19:03:18 -05:00
|
|
|
@log.close
|
2007-09-24 23:47:37 -04:00
|
|
|
end
|
2007-10-03 19:43:12 -04:00
|
|
|
|
2011-12-09 19:03:18 -05:00
|
|
|
private
|
|
|
|
def open_logfile(log)
|
|
|
|
Logger.new log
|
|
|
|
end
|
2007-09-24 23:47:37 -04:00
|
|
|
end
|
2007-10-03 19:43:12 -04:00
|
|
|
end
|