English spoken conference
WARNING: You are browsing the documentation for Symfony 4.2 which is not maintained anymore. Consider upgrading your projects to Symfony 4.3.

You are browsing the Symfony 4.2 documentation, which changes significantly from Symfony 3.x. If your app doesn't use Symfony 4.2 yet, browse the Symfony 3.4 documentation.

How to Configure Monolog to Exclude Specific HTTP Codes from the Log

4.2 version
Maintained Unmaintained
4.1

How to Configure Monolog to Exclude Specific HTTP Codes from the LogΒΆ

Sometimes your logs become flooded with unwanted HTTP errors, for example, 403s and 404s. When using a fingers_crossed handler, you can exclude logging these HTTP codes based on the MonologBundle configuration:

  • YAML
    1
    2
    3
    4
    5
    6
    7
    8
    # config/packages/prod/monolog.yaml
    monolog:
        handlers:
            main:
                # ...
                type: fingers_crossed
                handler: ...
                excluded_http_codes: [403, 404, { 400: ['^/foo', '^/bar'] }]
    
  • XML
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    <!-- config/packages/prod/monolog.xml -->
    <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 type="fingers_crossed" name="main" handler="...">
                <!-- ... -->
                <monolog:excluded-http-code code="403">
                    <monolog:url>^/foo</monolog:url>
                    <monolog:url>^/bar</monolog:url>
                </monolog:excluded-http-code>
                <monolog:excluded-http-code code="404"/>
            </monolog:handler>
        </monolog:config>
    </container>
    
  • PHP
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    // config/packages/prod/monolog.php
    $container->loadFromExtension('monolog', [
        'handlers' => [
            'main' => [
                // ...
                'type'                => 'fingers_crossed',
                'handler'             => ...,
                'excluded_http_codes' => [403, 404],
            ],
        ],
    ]);
    

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