Browse files

Allow users to provide custom formatters to Logger. [aeden] Closes #…

…7106, #2484

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6225 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent ad9bacb commit c9770d8a5ac1c477943ba431396e5d7e6b7389b0 @NZKoz NZKoz committed Feb 25, 2007
View
2 activesupport/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Allow users to provide custom formatters to Logger. [aeden]
+
* Hash#to_query CGI-escapes its keys. [Jeremy Kemper]
* Deprecation: warn on stderr if RAILS_DEFAULT_LOGGER isn't set yet. [Jeremy
View
48 activesupport/lib/active_support/clean_logger.rb
@@ -1,10 +1,21 @@
require 'logger'
require File.dirname(__FILE__) + '/core_ext/class/attribute_accessors'
-class Logger #:nodoc:
+# Extensions to the built in Ruby logger.
+#
+# If you want to use the default log formatter as defined in the Ruby core, then you
+# will need to set the formatter for the logger as in:
+#
+# logger.formatter = Formatter.new
+#
+# You can then specify the datetime format, for example:
+#
+# logger.datetime_format = "%Y-%m-%d"
+class Logger
+ # 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 = Logger::ERROR)
if silencer
@@ -18,6 +29,35 @@ def silence(temporary_level = Logger::ERROR)
yield self
end
end
+
+ alias :old_datetime_format= :datetime_format=
+ # Logging date-time format (string passed to +strftime+). Ignored if the formatter
+ # does not respond to datetime_format=.
+ def datetime_format=(datetime_format)
+ formatter.datetime_format = datetime_format if formatter.respond_to?(:datetime_format=)
+ end
+
+ alias :old_datetime_format :datetime_format
+ # Get the logging datetime format. Returns nil if the formatter does not support
+ # datetime formatting.
+ def datetime_format
+ formatter.datetime_format if formatter.respond_to?(:datetime_format)
+ end
+
+ alias :old_formatter :formatter
+ # Get the current formatter. The default formatter is a SimpleFormatter which only
+ # displays the log message
+ def formatter
+ @formatter ||= SimpleFormatter.new
+ end
+
+ # Simple formatter which only displays the message.
+ class SimpleFormatter < Logger::Formatter
+ # This method is invoked when a log event occurs
+ def call(severity, timestamp, progname, msg)
+ "#{msg}\n"
+ end
+ end
private
alias old_format_message format_message
@@ -28,11 +68,11 @@ def silence(temporary_level = Logger::ERROR)
# with Logger from 1.8.3 and vice versa.
if method_defined?(:formatter=)
def format_message(severity, timestamp, progname, msg)
- "#{msg}\n"
+ formatter.call(severity, timestamp, progname, msg)
end
else
def format_message(severity, timestamp, msg, progname)
- "#{msg}\n"
+ formatter.call(severity, timestamp, progname, msg)
end
end
end
View
7 activesupport/test/clean_logger_test.rb
@@ -40,6 +40,13 @@ def test_silence
assert_equal "error\nfatal\nerror\nfatal\nunsilenced\n", @out.string
end
+
+ def test_datetime_format
+ @logger.formatter = Logger::Formatter.new
+ @logger.datetime_format = "%Y-%m-%d"
+ @logger.debug 'debug'
+ assert_match(/D, \[\d\d\d\d-\d\d-\d\d#\d+\] DEBUG -- : debug/, @out.string)
+ end
end
class CleanLogger_182_to_183_Test < Test::Unit::TestCase

0 comments on commit c9770d8

Please sign in to comment.