forked from rails/rails
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure ActiveSupport::BroadcastLogger only executes blocks once.
[Fixes rails#49745] [Related rails#51883 rails#49771] Prior to this change, BroadcastLoggers would iterate each broadcast and execute the user provided block for each. This resulted in unintended behaviour since a user-provided block could execute multiple times. The consumer of any Logger would reasonably expect than when calling a method with a block, that block would only execute a single time. That is, the fact that a Logger is a BroadcastLogger should be irrelevant to consumer. The most significant example of this is with ActiveSupport::TaggedLogging. If a BroadcastLogger is used, and there are multiple loggers being broadcast to that respond to the `tagged` method, then calling `tagged` with a block would result in the block being called multiple times. For example: ```ruby broadcasts = ActiveSupport::BroadcastLogger.new( *Array.new(2) { ActiveSupport::TaggedLogging.logger } ) number = 0 broadcasts.tagged("FOO") { broadcasts.log(++number.to_s) } # Outputs: # [FOO] 1 # [FOO] 1 # [FOO] 2 # [FOO] 2 ``` The same issue also applies when calling `info`, `warn`, etc. with a block. This commit modifies the implementation used for dispatching to instead 'wrap' the block calls such that the user-provided block is only executed in the innermost call. (An assumption is made that when passed a block, all loggers will yield it, and have the same return semantics.) For example, the above example would effectively be executed as so: ```ruby broadcasts[0].tagged("FOO") { broadcasts[1].tagged("FOO") { yield } } ```
- Loading branch information
1 parent
13d5f87
commit f757877
Showing
3 changed files
with
77 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters