Monolog allows you to process every record before logging it by adding some extra data. This is the role of a processor, which can be applied for the whole handler stack or only for a specific handler or channel.
A processor is a callable receiving the record as its first argument. Processors are configured using the monolog.processor
DIC tag. See the reference about it.
Sometimes it is hard to tell which entries in the log belong to which session and/or request. The following example will add a unique token for each request using a processor:
Next, register your class as a service, as well as a formatter that uses the extra information:
Finally, set the formatter to be used on whatever handler you want:
If you use several handlers, you can also register a processor at the handler level or at the channel level instead of registering it globally (see the following sections).
When registering a new processor, instead of adding the tag manually in your configuration files, you can use the #[AsMonologProcessor]
attribute to apply it on the processor class:
The #[AsMonologProcessor]
attribute takes these optional arguments:
channel
: the logging channel the processor should be pushed to;handler
: the handler the processor should be pushed to;method
: the method that processes the records (useful when applying the attribute to the entire class instead of a single method).3.8
The #[AsMonologProcessor]
attribute was introduced in MonologBundle 3.8.
Symfony's MonologBridge provides processors that can be registered inside your application.
By default, processors are applied to all channels. Add the channel
option to the monolog.processor
tag to only apply a processor for the given channel:
This work, including the code samples, is licensed under a
Creative Commons BY-SA 3.0license.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4