A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://symfony.com/doc/current/logging/processors.html below:

How to Add extra Data to Log Messages via a Processor (Symfony Docs)

How to Add extra Data to Log Messages via a Processor

Edit this page

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.

Adding a Session/Request Token

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:

3.8

The #[AsMonologProcessor] attribute was introduced in MonologBundle 3.8.

Symfony's MonologBridge provides processors that can be registered inside your application.

DebugProcessor
Adds additional information useful for debugging like a timestamp or an error message to the record.
TokenProcessor
Adds information from the current user's token to the record namely username, roles and whether the user is authenticated.
SwitchUserTokenProcessor
Adds information about the user who is impersonating the logged in user, namely username, roles and whether the user is authenticated.
WebProcessor
Overrides data from the request using the data inside Symfony's request object.
RouteProcessor
Adds information about current route (controller, action, route parameters).
ConsoleCommandProcessor
Adds information about the current console command.
Registering Processors per Channel

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.0

license.


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