From c14b98c28a5962281dd3bc6fb2ca6c6cf79fbc2a Mon Sep 17 00:00:00 2001 From: Andrew Novoselac Date: Fri, 20 Oct 2023 10:26:03 -0400 Subject: [PATCH] Fix `BroadcastLogger#dup` so that it duplicates the logger's `broadcasts`. --- activesupport/CHANGELOG.md | 4 ++++ activesupport/lib/active_support/broadcast_logger.rb | 8 ++++++++ activesupport/test/broadcast_logger_test.rb | 12 ++++++++++++ 3 files changed, 24 insertions(+) diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index 38a1c9dd9e2b7..83f00e5d9b769 100644 --- a/activesupport/CHANGELOG.md +++ b/activesupport/CHANGELOG.md @@ -1,3 +1,7 @@ +* Fix `BroadcastLogger#dup` so that it duplicates the logger's `broadcasts`. + + *Andrew Novoselac* + * Fix issue where `bootstrap.rb` overwrites the `level` of a `BroadcastLogger`'s `broadcasts`. *Andrew Novoselac* diff --git a/activesupport/lib/active_support/broadcast_logger.rb b/activesupport/lib/active_support/broadcast_logger.rb index e5bf7232c115f..4d752faeed172 100644 --- a/activesupport/lib/active_support/broadcast_logger.rb +++ b/activesupport/lib/active_support/broadcast_logger.rb @@ -218,6 +218,14 @@ def fatal! dispatch { |logger| logger.fatal! } end + def initialize_copy(other) + @broadcasts = [] + @progname = other.progname.dup + @formatter = other.formatter.dup + + broadcast_to(*other.broadcasts.map(&:dup)) + end + private def dispatch(&block) @broadcasts.each { |logger| block.call(logger) } diff --git a/activesupport/test/broadcast_logger_test.rb b/activesupport/test/broadcast_logger_test.rb index 3bff7838e9de1..a5b4f189d4a93 100644 --- a/activesupport/test/broadcast_logger_test.rb +++ b/activesupport/test/broadcast_logger_test.rb @@ -290,6 +290,18 @@ def info(msg, &block) assert(logger.qux(param: "foo")) end + test "#dup duplicates the broadcasts" do + logger = CustomLogger.new + logger.level = ::Logger::WARN + broadcast_logger = BroadcastLogger.new(logger) + + duplicate = broadcast_logger.dup + + assert_equal ::Logger::WARN, duplicate.broadcasts.sole.level + assert_not_same logger, duplicate.broadcasts.sole + assert_same logger, broadcast_logger.broadcasts.sole + end + class CustomLogger attr_reader :adds, :closed, :chevrons attr_accessor :level, :progname, :formatter, :local_level