aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2007-09-25 03:47:37 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2007-09-25 03:47:37 +0000
commit79a9c7a702f4bccb2af5c82bb9a78209b28ab1c7 (patch)
treeabdd87ec54e111b54198cb70728e224da5bc3cfb /activesupport
parent501244fee4045dd7a48b8753e9f54a060fdc743d (diff)
downloadrails-79a9c7a702f4bccb2af5c82bb9a78209b28ab1c7.tar.gz
rails-79a9c7a702f4bccb2af5c82bb9a78209b28ab1c7.tar.bz2
rails-79a9c7a702f4bccb2af5c82bb9a78209b28ab1c7.zip
Added ActiveSupport::BufferedLogger as a duck-typing alternative (albeit with no formatter) to the Ruby Logger, which provides a very nice speed bump (inspired by Ezra's buffered logger) [DHH] Changed the default logger from Ruby's own Logger with the clean_logger extensions to ActiveSupport::BufferedLogger for performance reasons [DHH]. (You can change it back with config.logger = Logger.new(/path/to/log, level).)
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7626 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/CHANGELOG2
-rw-r--r--activesupport/lib/active_support.rb3
-rw-r--r--activesupport/lib/active_support/buffered_logger.rb79
-rw-r--r--activesupport/lib/active_support/clean_logger.rb2
4 files changed, 86 insertions, 0 deletions
diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG
index 9155e3cf27..ea31261c95 100644
--- a/activesupport/CHANGELOG
+++ b/activesupport/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Added ActiveSupport::BufferedLogger as a duck-typing alternative (albeit with no formatter) to the Ruby Logger, which provides a very nice speed bump (inspired by Ezra's buffered logger) [DHH]
+
* Object#instance_exec produces fewer garbage methods. [Mauricio Fernandez]
* Decode json strings as Dates/Times if they're using a YAML-compatible format. Closes #9614 [Rick]
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