How to Add extra Data to Log Messages via a Processor
Warning: You are browsing the documentation for Symfony 3.x, which is no longer maintained.
Read the updated version of this page for Symfony 7.1 (the current stable version).
Monolog allows you to process the record before logging it to add some extra data. A processor can be applied for the whole handler stack or only for a specific handler.
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
namespace AppBundle\Logger;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
class SessionRequestProcessor
{
private $session;
private $sessionId;
public function __construct(SessionInterface $session)
{
$this->session = $session;
}
public function __invoke(array $record)
{
if (!$this->session->isStarted()) {
return $record;
}
if (!$this->sessionId) {
$this->sessionId = substr($this->session->getId(), 0, 8) ?: '????????';
}
$record['extra']['token'] = $this->sessionId.'-'.substr(uniqid('', true), -8);
return $record;
}
}
Next, register your class as a service, as well as a formatter that uses the extra information:
1 2 3 4 5 6 7 8 9 10 11
# app/config/services.yml
services:
monolog.formatter.session_request:
class: Monolog\Formatter\LineFormatter
arguments:
- "[%%datetime%%] [%%extra.token%%] %%channel%%.%%level_name%%: %%message%% %%context%% %%extra%%\n"
AppBundle\Logger\SessionRequestProcessor:
autowire: true
tags:
- { name: monolog.processor }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
<!-- app/config/services.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:monolog="http://symfony.com/schema/dic/monolog"
xsi:schemaLocation="http://symfony.com/schema/dic/services
https://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/monolog
https://symfony.com/schema/dic/monolog/monolog-1.0.xsd">
<services>
<service id="monolog.formatter.session_request"
class="Monolog\Formatter\LineFormatter">
<argument>[%%datetime%%] [%%extra.token%%] %%channel%%.%%level_name%%: %%message%% %%context%% %%extra%%
</argument>
</service>
<service id="AppBundle\Logger\SessionRequestProcessor" autowire="true">
<tag name="monolog.processor"/>
</service>
</services>
</container>
1 2 3 4 5 6 7 8 9 10 11
// app/config/services.php
use AppBundle\Logger\SessionRequestProcessor;
use Monolog\Formatter\LineFormatter;
$container
->register('monolog.formatter.session_request', LineFormatter::class)
->addArgument('[%%datetime%%] [%%extra.token%%] %%channel%%.%%level_name%%: %%message%% %%context%% %%extra%%\n');
$container
->autowire(SessionRequestProcessor::class)
->addTag('monolog.processor', ['method' => 'processRecord']);
Finally, set the formatter to be used on whatever handler you want:
1 2 3 4 5 6 7 8
# app/config/config.yml
monolog:
handlers:
main:
type: stream
path: '%kernel.logs_dir%/%kernel.environment%.log'
level: debug
formatter: monolog.formatter.session_request
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
<!-- app/config/config.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:monolog="http://symfony.com/schema/dic/monolog"
xsi:schemaLocation="http://symfony.com/schema/dic/services
https://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/monolog
https://symfony.com/schema/dic/monolog/monolog-1.0.xsd">
<monolog:config>
<monolog:handler
name="main"
type="stream"
path="%kernel.logs_dir%/%kernel.environment%.log"
level="debug"
formatter="monolog.formatter.session_request"
/>
</monolog:config>
</container>
1 2 3 4 5 6 7 8 9 10 11
// app/config/config.php
$container->loadFromExtension('monolog', [
'handlers' => [
'main' => [
'type' => 'stream',
'path' => '%kernel.logs_dir%/%kernel.environment%.log',
'level' => 'debug',
'formatter' => 'monolog.formatter.session_request',
],
],
]);
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).
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.
- WebProcessor
- Overrides data from the request using the data inside Symfony's request object.
3.4
The TokenProcessor
class was introduced in Symfony 3.4.
Registering Processors per Handler
You can register a processor per handler using the handler
option of
the monolog.processor
tag:
1 2 3 4 5 6
# app/config/config.yml
services:
AppBundle\Logger\SessionRequestProcessor:
autowire: true
tags:
- { name: monolog.processor, handler: main }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
<!-- app/config/config.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:monolog="http://symfony.com/schema/dic/monolog"
xsi:schemaLocation="http://symfony.com/schema/dic/services
https://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/monolog
https://symfony.com/schema/dic/monolog/monolog-1.0.xsd">
<services>
<service id="AppBundle\Logger\SessionRequestProcessor" autowire="true">
<tag name="monolog.processor" handler="main"/>
</service>
</services>
</container>
1 2 3 4 5 6
// app/config/config.php
// ...
$container
->autowire(SessionRequestProcessor::class)
->addTag('monolog.processor', ['handler' => 'main']);
Registering Processors per Channel
You can register a processor per channel using the channel
option of
the monolog.processor
tag:
1 2 3 4 5 6
# app/config/config.yml
services:
AppBundle\Logger\SessionRequestProcessor:
autowire: true
tags:
- { name: monolog.processor, channel: main }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
<!-- app/config/config.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:monolog="http://symfony.com/schema/dic/monolog"
xsi:schemaLocation="http://symfony.com/schema/dic/services
https://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/monolog
https://symfony.com/schema/dic/monolog/monolog-1.0.xsd">
<services>
<service id="AppBundle\Logger\SessionRequestProcessor" autowire="true">
<tag name="monolog.processor" channel="main"/>
</service>
</services>
</container>
1 2 3 4 5 6
// app/config/config.php
// ...
$container
->autowire(SessionRequestProcessor::class)
->addTag('monolog.processor', ['channel' => 'main']);