How to Define a Custom Logging Formatter

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 easily. 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
     8
     9
    10
    11
    12
    13
    # app/config/services.yml
    services:
        # ...
    
        Monolog\Formatter\JsonFormatter: ~
    
    # app/config/config_prod.yml (and/or config_dev.yml)
    monolog:
        handlers:
            file:
                type: stream
                level: debug
                formatter: Monolog\Formatter\JsonFormatter
    
  • XML
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    <!-- 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
            http://symfony.com/schema/dic/services/services-1.0.xsd
            http://symfony.com/schema/dic/monolog
            http://symfony.com/schema/dic/monolog/monolog-1.0.xsd">
    
        <services>
            <service id="Monolog\Formatter\JsonFormatter" />
        </services>
    
        <!-- app/config/config_prod.xml (and/or config_dev.xml) -->
        <monolog:config>
            <monolog:handler
                name="file"
                type="stream"
                level="debug"
                formatter="Monolog\Formatter\JsonFormatter"
            />
        </monolog:config>
    </container>
    
  • PHP
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    // app/config/config.php
    use Monolog\Formatter\JsonFormatter;
    
    // app/config/services.php
    $container->register(JsonFormatter::class);
    
    // app/config/config_prod.php (or config_dev.php)
    $container->loadFromExtension('monolog', array(
        'handlers' => array(
            'file' => array(
                'type'      => 'stream',
                'level'     => 'debug',
                'formatter' => JsonFormatter::class',
            ),
        ),
    ));
    

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.