diff options
Diffstat (limited to 'activesupport')
-rw-r--r-- | activesupport/CHANGELOG | 2 | ||||
-rw-r--r-- | activesupport/lib/active_support/clean_logger.rb | 13 | ||||
-rw-r--r-- | activesupport/test/clean_logger_test.rb | 69 |
3 files changed, 79 insertions, 5 deletions
diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index ea645d8de0..d43011b9b8 100644 --- a/activesupport/CHANGELOG +++ b/activesupport/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Fix Logger compatibility for distributions that don't keep Ruby and its standard library in sync. + * Replace '%e' from long and short time formats as Windows does not support it. #2344. [Tom Ward <tom@popdog.net>] * Added to_s(:db) to Range, so you can get "BETWEEN '2005-12-10' AND '2005-12-12'" from Date.new(2005, 12, 10)..Date.new(2005, 12, 12) (and likewise with Times) diff --git a/activesupport/lib/active_support/clean_logger.rb b/activesupport/lib/active_support/clean_logger.rb index 07162f1d44..330dbb2e46 100644 --- a/activesupport/lib/active_support/clean_logger.rb +++ b/activesupport/lib/active_support/clean_logger.rb @@ -4,19 +4,22 @@ class Logger #:nodoc: # Silences the logger for the duration of the block. def silence(temporary_level = Logger::ERROR) old_logger_level, self.level = level, temporary_level - yield + yield self ensure self.level = old_logger_level end private - # Ruby 1.8.3 swapped the format_message params. - if RUBY_VERSION < '1.8.3' - def format_message(severity, timestamp, msg, progname) + # Ruby 1.8.3 transposed the msg and progname arguments to format_message. + # We can't test RUBY_VERSION because some distributions don't keep Ruby + # and its standard library in sync, leading to installations of Ruby 1.8.2 + # with Logger from 1.8.3 and vice versa. + if method_defined?(:formatter=) + def format_message(severity, timestamp, progname, msg) "#{msg}\n" end else - def format_message(severity, timestamp, progname, msg) + def format_message(severity, timestamp, msg, progname) "#{msg}\n" end end diff --git a/activesupport/test/clean_logger_test.rb b/activesupport/test/clean_logger_test.rb new file mode 100644 index 0000000000..0416b4a49e --- /dev/null +++ b/activesupport/test/clean_logger_test.rb @@ -0,0 +1,69 @@ +require 'test/unit' +require 'stringio' +require File.dirname(__FILE__) + '/../lib/active_support/clean_logger' + +class CleanLoggerTest < Test::Unit::TestCase + def setup + @out = StringIO.new + @logger = Logger.new(@out) + end + + def test_format_message + @logger.error 'error' + assert_equal "error\n", @out.string + end + + def test_silence + # Without yielding self. + @logger.silence do + @logger.debug 'debug' + @logger.info 'info' + @logger.warn 'warn' + @logger.error 'error' + @logger.fatal 'fatal' + end + + # Yielding self. + @logger.silence do |logger| + logger.debug 'debug' + logger.info 'info' + logger.warn 'warn' + logger.error 'error' + logger.fatal 'fatal' + end + + assert_equal "error\nfatal\nerror\nfatal\n", @out.string + end +end + +class CleanLogger_182_to_183_Test < Test::Unit::TestCase + def setup + if Logger.method_defined?(:formatter=) + Logger.send(:alias_method, :hide_formatter=, :formatter=) + else + Logger.send(:define_method, :formatter=) { } + end + load File.dirname(__FILE__) + '/../lib/active_support/clean_logger.rb' + + @out = StringIO.new + @logger = Logger.new(@out) + @logger.progname = 'CLEAN LOGGER TEST' + end + + def teardown + if Logger.method_defined?(:hide_formatter=) + Logger.send(:alias_method, :formatter=, :hide_formatter=) + else + Logger.send(:undef_method, :formatter=) + end + load File.dirname(__FILE__) + '/../lib/active_support/clean_logger.rb' + end + + # Since we've fooled Logger into thinking we're on 1.8.2 if we're on 1.8.3 + # and on 1.8.3 if we're on 1.8.2, it'll define format_message with the + # wrong order of arguments and therefore print progname instead of msg. + def test_format_message_with_faked_version + @logger.error 'error' + assert_equal "CLEAN LOGGER TEST\n", @out.string + end +end |