Repository navigation
[9.x] Share logging context across channels and stacks - #42276
Conversation
|
How can we reset this behavior? |
|
Regarding Octane, you would need to hook into events in the $this->app['events']->listen([
RequestReceived::class,
TaskReceived::class,
TickReceived::class,
], fn () => Log::shareContext(['invocation-id' => (string) Str::uuid()]));We could potentially add some magic into Octane to handle this for you, document it on the Octane documentation, or allow the logger to know about Octane and handle this internally. I didn't add the ability to reset the shared context as I don't have a use-case for that and didn't want to add the feature without one. The service provider is also just an example. It would continue to be possible to register a "request-id" in a middleware with this new feature. |
|
@timacdonald we would likely need a corresponding PR in Octane to reset the shared context back to its pre-request state after each operation (see other listeners in Octane as examples of this). Marking as draft until we have that worked up as well. 👍 |
|
@taylorotwell see: 3e5e28e && laravel/octane#513 note that the new test will fail until this PR is merged / tagged. I've tested locally and got the ✅ |
|
I just tested the PS: I tested this on Laravel 10. |
* allow context to be shared across channels and stacks * add ability to flush the shared context * formatting " Co-authored-by: Taylor Otwell <taylor@laravel.com>
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
withContextfeature 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
withContexti.e. not recursive.