|
| 1 | +.. index:: |
| 2 | + single: Logging |
| 3 | + single: Logging; Exclude HTTP Codes |
| 4 | + single: Monolog; Exclude HTTP Codes |
| 5 | + |
| 6 | +How to Configure Monolog to Exclude Specific HTTP Codes from the Log |
| 7 | +==================================================================== |
| 8 | + |
| 9 | +..versionadded:: 4.1 |
| 10 | + The ability to exclude log messages based on their status codes was |
| 11 | + introduced in Symfony 4.1 and MonologBundle 3.3. |
| 12 | + |
| 13 | +Sometimes your logs become flooded with unwanted HTTP errors, for example, |
| 14 | +403s and 404s. When using a ``fingers_crossed`` handler, you can exclude |
| 15 | +logging these HTTP codes based on the MonologBundle configuration: |
| 16 | + |
| 17 | +.. configuration-block:: |
| 18 | + |
| 19 | + .. code-block:: yaml |
| 20 | +
|
| 21 | + # config/packages/prod/monolog.yaml |
| 22 | + monolog: |
| 23 | + handlers: |
| 24 | + main: |
| 25 | + # ... |
| 26 | + type: fingers_crossed |
| 27 | + handler: ... |
| 28 | + excluded_http_codes: [403, 404, { 400: ['^/foo', '^/bar'] }] |
| 29 | +
|
| 30 | + .. code-block:: xml |
| 31 | +
|
| 32 | + <!-- config/packages/prod/monolog.xml --> |
| 33 | + <container xmlns="http://symfony.com/schema/dic/services" |
| 34 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 35 | + xmlns:monolog="http://symfony.com/schema/dic/monolog" |
| 36 | + xsi:schemaLocation="http://symfony.com/schema/dic/services |
| 37 | + http://symfony.com/schema/dic/services/services-1.0.xsd |
| 38 | + http://symfony.com/schema/dic/monolog |
| 39 | + http://symfony.com/schema/dic/monolog/monolog-1.0.xsd"> |
| 40 | +
|
| 41 | + <monolog:config> |
| 42 | + <monolog:handler type="fingers_crossed" name="main" handler="..."> |
| 43 | + <!-- ... --> |
| 44 | + <monolog:excluded-http-code code="403"> |
| 45 | + <monolog:url>^/foo</monolog:url> |
| 46 | + <monolog:url>^/bar</monolog:url> |
| 47 | + </monolog:excluded-http-code> |
| 48 | + <monolog:excluded-http-code code="404" /> |
| 49 | + </monolog:handler> |
| 50 | + </monolog:config> |
| 51 | + </container> |
| 52 | +
|
| 53 | + .. code-block:: php |
| 54 | +
|
| 55 | + // config/packages/prod/monolog.php |
| 56 | + $container->loadFromExtension('monolog', array( |
| 57 | + 'handlers' => array( |
| 58 | + 'main' => array( |
| 59 | + // ... |
| 60 | + 'type' => 'fingers_crossed', |
| 61 | + 'handler' => ..., |
| 62 | + 'excluded_http_codes' => array(403, 404), |
| 63 | + ), |
| 64 | + ), |
| 65 | + )); |
0 commit comments