Permalink
Browse files

Allow wrapping a logger that hasn't set a formatter. Default to our S…

…impleFormatter. Otherwise we try extending nil with the tagging API.
  • Loading branch information...
1 parent 8156178 commit 107fd8788f92da53232e87a220d6f4329c8f2b13 @jeremy jeremy committed Sep 26, 2012
Showing with 12 additions and 2 deletions.
  1. +4 −2 activesupport/lib/active_support/tagged_logging.rb
  2. +8 −0 activesupport/test/tagged_logging_test.rb
@@ -21,10 +21,10 @@ def call(severity, timestamp, progname, msg)
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 @@ def tags_text
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
@@ -14,6 +14,14 @@ def flush(*)
@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

0 comments on commit 107fd87

Please sign in to comment.