mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Allow wrapping a logger that hasn't set a formatter. Default to our SimpleFormatter. Otherwise we try extending nil with the tagging API.
This commit is contained in:
parent
8156178894
commit
107fd8788f
2 changed files with 12 additions and 2 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue