diff options
Diffstat (limited to 'activesupport/lib')
-rw-r--r-- | activesupport/lib/active_support.rb | 3 | ||||
-rw-r--r-- | activesupport/lib/active_support/buffered_logger.rb | 79 | ||||
-rw-r--r-- | activesupport/lib/active_support/clean_logger.rb | 2 |
3 files changed, 84 insertions, 0 deletions
diff --git a/activesupport/lib/active_support.rb b/activesupport/lib/active_support.rb index 9eec6f3b38..28424dd6a7 100644 --- a/activesupport/lib/active_support.rb +++ b/activesupport/lib/active_support.rb @@ -29,7 +29,10 @@ require 'builder' require 'active_support/inflector' require 'active_support/core_ext' + require 'active_support/clean_logger' +require 'active_support/buffered_logger' + require 'active_support/dependencies' require 'active_support/deprecation' diff --git a/activesupport/lib/active_support/buffered_logger.rb b/activesupport/lib/active_support/buffered_logger.rb new file mode 100644 index 0000000000..b28f4a5ad3 --- /dev/null +++ b/activesupport/lib/active_support/buffered_logger.rb @@ -0,0 +1,79 @@ +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 + + # 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 + + attr_accessor :level, :auto_flushing + attr_reader :buffer + + def initialize(log, level = DEBUG) + @level = level + @buffer = "" + @auto_flushing = true + + if log.respond_to?(:write) + @log = log + elsif File.exist?(log) + @log = open(log, (File::WRONLY | File::APPEND)) + @log.sync = true + else + @log = open(log, (File::WRONLY | File::APPEND | File::CREAT)) + @log.sync = true + @log.write("# Logfile created on %s" % [Time.now.to_s]) + end + end + + for severity in Severity.constants + class_eval <<-EOT + def #{severity.downcase}(message) + return if @level > #{severity} + message << "\\n" unless message[-1] == ?\\n + @buffer << message + flush if auto_flushing + message + end + + def #{severity.downcase}? + @level == #{severity} + end + EOT + end + + def flush + return if @buffer.size == 0 + @log.write(@buffer.slice!(0..-1)) + end + + def close + flush + @log.close if @log.respond_to?(:close) + @log = nil + end + end +end
\ No newline at end of file diff --git a/activesupport/lib/active_support/clean_logger.rb b/activesupport/lib/active_support/clean_logger.rb index 95d4f07f4d..0c6de67f8d 100644 --- a/activesupport/lib/active_support/clean_logger.rb +++ b/activesupport/lib/active_support/clean_logger.rb @@ -11,6 +11,8 @@ require File.dirname(__FILE__) + '/core_ext/class/attribute_accessors' # You can then specify the datetime format, for example: # # logger.datetime_format = "%Y-%m-%d" +# +# Note: This logger is deprecated in favor of ActiveSupport::BufferedLogger class Logger # Set to false to disable the silencer cattr_accessor :silencer |