Symfony

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

Install

Install the sentry/sentry-symfony package:

Copied
composer require sentry/sentry-symfony

Configure

Add your DSN to config/packages/sentry.yaml:

config/packages/sentry.yaml
Copied
sentry:
    dsn: "%env(SENTRY_DSN)%"

And in your .env file:

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

If you are not using Symfony Flex, you'll also need to enable the bundle in config/bundles.php:

config/bundles.php
Copied
<?php

return [
    // ...
    Sentry\SentryBundle\SentryBundle::class => ['all' => true],
];

Monolog Integration

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

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

If you are using a version of MonologBundle prior to 3.7, you need to configure the handler as a service instead:

config/packages/sentry.yaml
Copied
monolog:
    handlers:
        sentry:
            type: service
            id: Sentry\Monolog\Handler

services:
    Sentry\Monolog\Handler:
        arguments:
            $hub: '@Sentry\State\HubInterface'
            $level: !php/const Monolog\Logger::ERROR

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 }

Test the implementation

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\Component\Routing\Annotation\Route;

class SentryTestController
{
    /**
     * @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.