Skip to content

Commit

Permalink
Fix BroadcastLogger#dup so that it duplicates the logger's `broadca…
Browse files Browse the repository at this point in the history
…sts`.
  • Loading branch information
andrewn617 committed Oct 23, 2023
1 parent 7de3400 commit c14b98c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
4 changes: 4 additions & 0 deletions activesupport/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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*
Expand Down
8 changes: 8 additions & 0 deletions activesupport/lib/active_support/broadcast_logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand Down
12 changes: 12 additions & 0 deletions activesupport/test/broadcast_logger_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c14b98c

Please sign in to comment.