Permalink
Browse files

r3574@sedna: jeremy | 2005-10-13 11:16:51 -0700

 More precise Logger version test to account for distributions that don't keep Ruby and its stdlib in sync.  Logger#silence yields self for convenience.  Logger unit tests.


git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2553 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 634da0f commit 9c8227973cd5fc00f1df199756220712ae78f6dc @jeremy jeremy committed Oct 13, 2005
@@ -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)
@@ -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
@@ -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

0 comments on commit 9c82279

Please sign in to comment.