Shaun Simmons
Contributed by Shaun Simmons in #23707

Logging as much information as possible is essential to help you debug the issues found in your applications. However, logging too much information can be as bad as logging too little, because of all the "noise" added to your logs.

That's why in Symfony 4.1 we've improved the Monolog integration to allow you exclude log messages related to specific HTTP codes. For example, when using a fingers_crossed handler, use the following configuration to ignore the logs about 403 and 404 errors:

1
2
3
4
5
6
7
# config/packages/monolog.yaml
monolog:
    handlers:
        main:
            # ...
            type: 'fingers_crossed'
            excluded_http_codes: [403, 404]

For more complex needs, it's also possible to exclude logs only for certain URLs, defined as regular expression patterns:

1
2
3
4
5
6
# config/packages/monolog.yaml
monolog:
    handlers:
        main:
            # ...
            excluded_http_codes: [{ 400: ['^/foo', '^/bar'] }, 403, 404]

If you prefer XML configuration, this is how the previous example would look like:

1
2
3
4
5
6
7
8
9
10
11
12
<!-- config/packages/monolog.xml -->
<monolog:config>
    <monolog:handler type="fingers_crossed" name="main" handler="...">
        <!-- ... -->
        <monolog:excluded-http-code code="400">
            <monolog:url>^/foo</monolog:url>
            <monolog:url>^/bar</monolog:url>
        </monolog:excluded-http-code>
        <monolog:excluded-http-code code="403" />
        <monolog:excluded-http-code code="404" />
    </monolog:handler>
</monolog:config>
Published in #Living on the edge