'Sentry on Symfony: how to exclude `NotFoundHttpException`
I'm using the SentryBundle to integrate Sentry in my Symfony app.
I don't want to record "NotFoundExceptions", so I configured the bundle this way:
sentry:
dsn: '%env(SENTRY_DSN)%'
register_error_listener: false # Disables the ErrorListener
monolog:
error_handler:
enabled: true
level: error
messenger:
enabled: true # flushes Sentry messages at the end of each message handling
capture_soft_fails: true # captures exceptions marked for retry too
options:
environment: '%kernel.environment%'
# release: '%env(VERSION)%' #your app version
excluded_exceptions:
- Symfony\Component\HttpKernel\Exception\NotFoundHttpException
Unfortunately, this seems to be not sufficient as the exceptions continue to be logged in Sentry.
What am I doing wrong?
Solution 1:[1]
As excluded_exceptions
is not available inside the bundle / sdk anymore and you dont what eg "NotFoundHttpException". you can simply use excluded_http_codes
with a wrapped monolog handler.
full example
sentry:
register_error_listener: false # custom monolog handler: Disables the ErrorListener to avoid duplicated log in sentry
dsn: 'FOOBAR_DNS'
options:
traces_sample_rate: 0.1
tracing:
enabled: true
dbal:
enabled: true
connection: 'default'
cache:
enabled: true
twig:
enabled: true
# sentry: log also error logs when there is no request exception
monolog:
handlers:
sentry:
type: fingers_crossed
action_level: error
handler: sentry_main
excluded_http_codes: [ 404, 405 ]
buffer_size: 50
sentry_main:
type: sentry
level: error
hub_id: Sentry\State\HubInterface
Solution 2:[2]
For the recent versions of Sentry and Sentry bundle you need to create an entry for the Sentry\Integration\IgnoreErrorsIntegration
service.
Update your services.yaml
with:
services:
<...other services>
Sentry\Integration\IgnoreErrorsIntegration:
arguments:
$options:
ignore_exceptions:
- Symfony\Component\HttpKernel\Exception\NotFoundHttpException
After that to tell Sentry to use this integration you need to update your sentry.yaml
file to include the IgnoreErrorsIntegration
:
sentry:
dsn: '%env(SENTRY_DSN)%'
options:
integrations:
- 'Sentry\Integration\IgnoreErrorsIntegration'
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Daniel Espendiller |
Solution 2 |