Add Sentry to Symfony Project
17 August 2025 (Updated 17 August 2025)
On this page
Install package
composer require sentry/sentry-symfony
Configure Sentry
Add your SENTRY_DSN
to your .env
file:
###> sentry/sentry-symfony ###
SENTRY_DSN="<your-sentry-dsn>"
###< sentry/sentry-symfony ###
Test integration works ok
Add a test route:
<?php
namespace App\Controller;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Attribute\Route;
class SentryTestController extends AbstractController
{
public function __construct(private LoggerInterface $logger)
{
}
#[Route('/_sentry-test', name: 'sentry_test')]
public function testLog()
{
// the following code will test if monolog integration logs to sentry
$this->logger->error('My custom logged error.', ['some' => 'Context Data']);
// the following code will test if an uncaught exception logs to sentry
throw new \RuntimeException('Example exception.');
}
}
By default, Sentry is enabled only in the prod
environment but you can make testing it easier on development by doing the following:
1) Edit config/packages.sentry.yaml
to remove the when@prod
key.
2) Edit config/bundles.php
to change:
Sentry\SentryBundle\SentryBundle::class => ['prod' => true],
to:
Sentry\SentryBundle\SentryBundle::class => ['all' => true],
If you don’t want to enable Sentry in development, just make sure that the SENTRY_DSN
env variable is set to an empty string in dev.
Links
Tagged:
Symfony recipes