Symfony

Symfony is supported via the sentry-symfony package as a native bundle.

Install the sentry/sentry-symfony bundle:

Copied
composer require sentry/sentry-symfony

Add your DSN to your .env file:

.env
Copied
###> sentry/sentry-symfony ###
SENTRY_DSN="https://examplePublicKey@o0.ingest.sentry.io/0"
###< sentry/sentry-symfony ###

If you are using Monolog to report events instead of the typical error listener approach, you need this additional configuration to log the errors correctly:

config/packages/sentry.yaml
Copied
sentry:
    register_error_listener: false # Disables the ErrorListener to avoid duplicated log in sentry
    register_error_handler: false # Disables the ErrorListener, ExceptionListener and FatalErrorListener integrations of the base PHP SDK

monolog:
    handlers:
        sentry:
            type: sentry
            level: !php/const Monolog\Logger::ERROR
            hub_id: Sentry\State\HubInterface

Additionally, you can register the PsrLogMessageProcessor to resolve PSR-3 placeholders in reported messages:

config/packages/sentry.yaml
Copied
services:
    Monolog\Processor\PsrLogMessageProcessor:
        tags: { name: monolog.processor, handler: sentry }

To test that both logger error and exception are correctly sent to sentry.io, you can create the following controller:

Copied
<?php

namespace App\Controller;

use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;

class SentryTestController extends AbstractController
{
    /**
     * @var LoggerInterface
     */
    private $logger;

    public function __construct(LoggerInterface $logger)
    {
        $this->logger = $logger;
    }

    /**
     * @Route(name="sentry_test", path="/_sentry-test")
     */
    public function testLog()
    {
        // the following code will test if monolog integration logs to sentry
        $this->logger->error('My custom logged error.');

        // the following code will test if an uncaught exception logs to sentry
        throw new \RuntimeException('Example exception.');
    }
}

After you visit the /_sentry-test page, you can view and resolve the recorded error by logging into sentry.io and opening your project. Clicking on the error's title will open a page where you can see detailed information and mark it as resolved.

Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").