How to Define a Custom Logging FormatterΒΆ

Each logging handler uses a Formatter to format the record before logging it. All Monolog handlers use an instance of Monolog\Formatter\LineFormatter by default but you can replace it. Your formatter must implement Monolog\Formatter\FormatterInterface.

For example, to use the built-in JsonFormatter, register it as a service then configure your handler to use it:

  • YAML
    1
    2
    3
    4
    5
    6
    7
    # config/packages/prod/monolog.yaml (and/or config/packages/dev/monolog.yaml)
    monolog:
        handlers:
            file:
                type: stream
                level: debug
                formatter: 'monolog.formatter.json'
    
  • XML
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    <!-- 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">
    
        <!-- config/packages/prod/monolog.xml (and/or config/packages/dev/monolog.xml) -->
        <monolog:config>
            <monolog:handler
                name="file"
                type="stream"
                level="debug"
                formatter="monolog.formatter.json"
            />
        </monolog:config>
    </container>
    
  • PHP
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    // config/services.php
    use Monolog\Formatter\JsonFormatter;
    
    // config/packages/prod/monolog.php (and/or config/packages/dev/monolog.php)
    $container->loadFromExtension('monolog', [
        'handlers' => [
            'file' => [
                'type'      => 'stream',
                'level'     => 'debug',
                'formatter' => 'monolog.formatter.json',
            ],
        ],
    ]);