Permalink
Browse files

Ensure clean_logger fixes are 1.8.2 compatible [mislav] Closes #8396

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7083 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent e682fee commit 3bf3042a6cd29fd3ccc18541e84ea8ca5c791889 @NZKoz NZKoz committed Jun 22, 2007
@@ -1,5 +1,7 @@
*SVN*
+* Backport clean_logger changes to support ruby 1.8.2 [mislav]
+
* Added proper handling of arrays #8537 [hasmanyjosh]
Before:
@@ -44,12 +44,50 @@ def datetime_format
formatter.datetime_format if formatter.respond_to?(:datetime_format)
end
- alias :old_formatter :formatter
+ alias :old_formatter :formatter if method_defined?(:formatter)
# Get the current formatter. The default formatter is a SimpleFormatter which only
# displays the log message
def formatter
@formatter ||= SimpleFormatter.new
end
+
+ unless const_defined? :Formatter
+ class Formatter
+ Format = "%s, [%s#%d] %5s -- %s: %s\n"
+
+ attr_accessor :datetime_format
+
+ def initialize
+ @datetime_format = nil
+ end
+
+ def call(severity, time, progname, msg)
+ Format % [severity[0..0], format_datetime(time), $$, severity, progname,
+ msg2str(msg)]
+ end
+
+ private
+ def format_datetime(time)
+ if @datetime_format.nil?
+ time.strftime("%Y-%m-%dT%H:%M:%S.") << "%06d " % time.usec
+ else
+ time.strftime(@datetime_format)
+ end
+ end
+
+ def msg2str(msg)
+ case msg
+ when ::String
+ msg
+ when ::Exception
+ "#{ msg.message } (#{ msg.class })\n" <<
+ (msg.backtrace || []).join("\n")
+ else
+ msg.inspect
+ end
+ end
+ end
+ end
# Simple formatter which only displays the message.
class SimpleFormatter < Logger::Formatter
@@ -74,5 +112,14 @@ def format_message(severity, timestamp, progname, msg)
def format_message(severity, timestamp, msg, progname)
formatter.call(severity, timestamp, progname, msg)
end
+
+ attr_writer :formatter
+ public :formatter=
+
+ alias old_format_datetime format_datetime
+ def format_datetime(datetime) datetime end
+
+ alias old_msg2str msg2str
+ def msg2str(msg) msg end
end
end
@@ -48,40 +48,3 @@ def test_datetime_format
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
- def setup
- silence_warnings do
- if Logger.method_defined?(:formatter=)
- Logger.send(:alias_method, :hide_formatter=, :formatter=)
- Logger.send(:undef_method, :formatter=)
- else
- Logger.send(:define_method, :formatter=) { }
- end
- load File.dirname(__FILE__) + '/../lib/active_support/clean_logger.rb'
- end
-
- @out = StringIO.new
- @logger = Logger.new(@out)
- @logger.progname = 'CLEAN LOGGER TEST'
- end
-
- def teardown
- silence_warnings do
- 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
- 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 3bf3042

Please sign in to comment.