From 107fd8788f92da53232e87a220d6f4329c8f2b13 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Wed, 26 Sep 2012 10:52:19 -0700 Subject: Allow wrapping a logger that hasn't set a formatter. Default to our SimpleFormatter. Otherwise we try extending nil with the tagging API. --- activesupport/lib/active_support/tagged_logging.rb | 6 ++++-- activesupport/test/tagged_logging_test.rb | 8 ++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) (limited to 'activesupport') diff --git a/activesupport/lib/active_support/tagged_logging.rb b/activesupport/lib/active_support/tagged_logging.rb index 8af6fa4ba7..33810442da 100644 --- a/activesupport/lib/active_support/tagged_logging.rb +++ b/activesupport/lib/active_support/tagged_logging.rb @@ -21,10 +21,10 @@ module ActiveSupport end def tagged(*tags) - new_tags = push_tags *tags + new_tags = push_tags(*tags) yield self ensure - pop_tags new_tags.size + pop_tags(new_tags.size) end def push_tags(*tags) @@ -55,6 +55,8 @@ module ActiveSupport end def self.new(logger) + # Ensure we set a default formatter so we aren't extending nil! + logger.formatter ||= ActiveSupport::Logger::SimpleFormatter.new logger.formatter.extend Formatter logger.extend(self) end diff --git a/activesupport/test/tagged_logging_test.rb b/activesupport/test/tagged_logging_test.rb index fa045baa28..27f629474e 100644 --- a/activesupport/test/tagged_logging_test.rb +++ b/activesupport/test/tagged_logging_test.rb @@ -14,6 +14,14 @@ class TaggedLoggingTest < ActiveSupport::TestCase @logger = ActiveSupport::TaggedLogging.new(MyLogger.new(@output)) end + test 'sets logger.formatter if missing and extends it with a tagging API' do + logger = Logger.new(StringIO.new) + assert_nil logger.formatter + ActiveSupport::TaggedLogging.new(logger) + assert_not_nil logger.formatter + assert logger.formatter.respond_to?(:tagged) + end + test "tagged once" do @logger.tagged("BCX") { @logger.info "Funky time" } assert_equal "[BCX] Funky time\n", @output.string -- cgit v1.2.3