Clone to keep extended Logger methods for tagged logger

`#dup` resets the extended Logger methods that could come from enabling broadcasting. That would mean if we create a tagged logger from a Logger with broadcasting enabled (usually to stdout), the new tagged logger will not perform broadcasting.
This commit is contained in:
Orhan Toy 2020-12-07 14:43:37 +01:00
parent 9b6008924d
commit 70af536b5d
2 changed files with 13 additions and 1 deletions

View File

@ -79,7 +79,7 @@ module ActiveSupport
end
def self.new(logger)
logger = logger.dup
logger = logger.clone
if logger.formatter
logger.formatter = logger.formatter.dup

View File

@ -214,4 +214,16 @@ class TaggedLoggingWithoutBlockTest < ActiveSupport::TestCase
assert_equal "[BCX] [Jason] Funky time\n[BCX] Junky time!\n", @output.string
end
test "keeps broadcasting functionality" do
broadcast_output = StringIO.new
broadcast_logger = ActiveSupport::TaggedLogging.new(Logger.new(broadcast_output))
@logger.extend(ActiveSupport::Logger.broadcast(broadcast_logger))
tagged_logger = @logger.tagged("OMG")
tagged_logger.info "Broadcasting..."
assert_equal "[OMG] Broadcasting...\n", @output.string
assert_equal "[OMG] Broadcasting...\n", broadcast_output.string
end
end