From 70af536b5d6e57800cb6b6b9a10f33b39192113e Mon Sep 17 00:00:00 2001 From: Orhan Toy Date: Mon, 7 Dec 2020 14:43:37 +0100 Subject: [PATCH] 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. --- activesupport/lib/active_support/tagged_logging.rb | 2 +- activesupport/test/tagged_logging_test.rb | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/activesupport/lib/active_support/tagged_logging.rb b/activesupport/lib/active_support/tagged_logging.rb index ed551011a4..0bc4a21401 100644 --- a/activesupport/lib/active_support/tagged_logging.rb +++ b/activesupport/lib/active_support/tagged_logging.rb @@ -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 diff --git a/activesupport/test/tagged_logging_test.rb b/activesupport/test/tagged_logging_test.rb index 959603c92a..fb9aca78e3 100644 --- a/activesupport/test/tagged_logging_test.rb +++ b/activesupport/test/tagged_logging_test.rb @@ -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