Skip to content
  • About
    • What is Symfony?
    • Community
    • News
    • Contributing
    • Support
  • Documentation
    • Symfony Docs
    • Symfony Book
    • Screencasts
    • Symfony Bundles
    • Symfony Cloud
    • Training
  • Services
    • SensioLabs Professional services to help you with Symfony
    • Platform.sh for Symfony Best platform to deploy Symfony apps
    • SymfonyInsight Automatic quality checks for your apps
    • Symfony Certification Prove your knowledge and boost your career
    • Blackfire Profile and monitor performance of your apps
  • Other
  • Blog
  • Download
sponsored by SensioLabs
  1. Home
  2. Documentation
  3. Logging
  4. How to Configure Monolog to Exclude Specific HTTP Codes from the Log
  • Documentation
  • Book
  • Reference
  • Bundles
  • Cloud

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

Edit this page

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:

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'] }]
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>
1
2
3
4
5
6
7
8
9
10
11
12
13
// config/packages/prod/monolog.php
use Symfony\Config\MonologConfig;

return static function (MonologConfig $monolog) {
    $mainHandler = $monolog->handler('main')
        // ...
        ->type('fingers_crossed')
        ->handler('...')
    ;

    $mainHandler->excludedHttpCode()->code(403);
    $mainHandler->excludedHttpCode()->code(404);
};

Caution

Combining excluded_http_codes with a passthru_level lower than error (i.e. debug, info, notice or warning) will not actually exclude log messages for those HTTP codes because they are logged with level of error or higher and passthru_level takes precedence over the HTTP codes being listed in excluded_http_codes.

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
TOC
    Version
    We stand with Ukraine.
    Version:

    Symfony 6.2 is backed by

    Symfony 6.2 is backed by

    Be safe against critical risks to your projects and businesses

    Be safe against critical risks to your projects and businesses

    Be trained by SensioLabs experts (2 to 6 day sessions -- French or English).

    Be trained by SensioLabs experts (2 to 6 day sessions -- French or English).

    Symfony footer

    ↓ Our footer now uses the colors of the Ukrainian flag because Symfony stands with the people of Ukraine.

    Avatar of Axel Guckelsberger, a Symfony contributor

    Thanks Axel Guckelsberger (@guite) for being a Symfony contributor

    6 commits • 146 lines changed

    View all contributors that help us make Symfony

    Become a Symfony contributor

    Be an active part of the community and contribute ideas, code and bug fixes. Both experts and newcomers are welcome.

    Learn how to contribute

    Symfony™ is a trademark of Symfony SAS. All rights reserved.

    • What is Symfony?

      • Symfony at a Glance
      • Symfony Components
      • Case Studies
      • Symfony Releases
      • Security Policy
      • Logo & Screenshots
      • Trademark & Licenses
      • symfony1 Legacy
    • Learn Symfony

      • Symfony Docs
      • Symfony Book
      • Reference
      • Bundles
      • Best Practices
      • Training
      • eLearning Platform
      • Certification
    • Screencasts

      • Learn Symfony
      • Learn PHP
      • Learn JavaScript
      • Learn Drupal
      • Learn RESTful APIs
    • Community

      • SymfonyConnect
      • Support
      • How to be Involved
      • Code of Conduct
      • Events & Meetups
      • Projects using Symfony
      • Downloads Stats
      • Contributors
      • Backers
    • Blog

      • Events & Meetups
      • A week of symfony
      • Case studies
      • Cloud
      • Community
      • Conferences
      • Diversity
      • Documentation
      • Living on the edge
      • Releases
      • Security Advisories
      • SymfonyInsight
      • Twig
      • SensioLabs
    • Services

      • SensioLabs services
      • Train developers
      • Manage your project quality
      • Improve your project performance
      • Host Symfony projects

      Deployed on

    Follow Symfony

    Search by Algolia