[9.x] Share logging context across channels and stacks #42276
Merged
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.
Purpose
This PR bring the ability to share context across all channels and stacks, rather than just one.
Why this feature is useful
Currently when we specify context, it is on a per log or per channel basis.
but there is no way to specify context to be shared across all channels and stacks.
This feature is useful in the example currently shown in the docs for the
withContext
feature of a channel.The example in the docs being...
The problem with this is that it only applies the context the the default channel, but it would be useful if the context was shared across ALL channels and stacks.
Usage
This new feature could be used in a middleware as above, but also in a service provider to apply a unique ID to all invocations of the app, be it from a request or a job or an artisan command.
Now in my application, no matter what logger I use, I'll have the invocation ID in my context.
Other logging systems outside of the Laravel logging system can also hook in and get the shared context to utilise...
Notes
withContext
i.e. not recursive.