BroadcastLogger should support TaggedLogging correctly #53105
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
[Fixes #49745 #52876]
[Related #51883 #49771 #53093]
NB: I have a related pull request in #53093 that refactors BroadcastLogger to ensure that
block forms of delegated methods also only execute the block once.
This commit extends BroadcastLogger when TaggedLogging is loaded in
order to ensure that BroadcastLogger#tagged behaves as expected when
there are multiple loggers being broadcast to.
TaggedLogging provides two related, but distinct interfaces:
Calling
logger.tagged(*tags)
without a block returns a new Loggerwith the provided tags pushed to its tag stack.
Calling
logger.tagged(*tags) { |logger| ... }
with a block pushesthe tags to the existing Logger and yields the logger to block.
Previously, BroadcastLogger would only function as expected if there
was just one Logger being broadcast to. When there were multiple
loggers: when calling
tagged
with a block, it would yield a blockmultiple times, once for each logger; when called without a block, it
would call
tagged
on each logger and return an Array of the results.This inconsistent behaviour has caused issues such as those referenced
above. In development environments in particular, logger configuration
can often result in a BroadcastLogger with two loggers, since the
bin/rails server
command will always broadcast to STDOUT, resultingin confusing behaviour.
This commit provides a
BroadcastLogger#tagged
implementation that,for the non-block form returns a new BroadcastLogger broadcasting to
the new tagged Loggers, and for the block-form, it 'wraps' the
user-provided block within nested calls to
TaggedLogging#logged
.The user-provided block is only executed in the innermost call.
For example:
Would execute similarly to: