Symfony

Symfony is a set of reusable PHP components and a PHP framework to build web applications and services. Learn how to set it up with Sentry.

  • You need a Sentry account and project
  • Your Symfony application needs to run on PHP 7.2 or later

Install the sentry/sentry-symfony bundle:

Copied
composer require 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.

Add your DSN to your .env file:

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

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.', ['some' => 'Context Data']);

        // 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").