Symfony
Symfony is supported via the sentry-symfony
package as a native bundle.
Install
Install the sentry/sentry-symfony
package:
composer require sentry/sentry-symfony
Due to a bug in all versions below 6.0
of the SensioFrameworkExtraBundle
bundle, you will likely receive an error during the execution of the command above related to the missing Nyholm\Psr7\Factory\Psr17Factory
class. To workaround the issue, if you are not using the PSR-7 bridge, please change the configuration of that bundle as follows:
sensio_framework_extra:
psr_message:
enabled: false
For more details about the issue see https://github.com/sensiolabs/SensioFrameworkExtraBundle/pull/710.
Configure
Add your DSN to config/packages/sentry.yaml
:
config/packages/sentry.yaml
sentry:
dsn: "%env(SENTRY_DSN)%"
And in your .env
file:
.env
###> sentry/sentry-symfony ###
SENTRY_DSN="https://examplePublicKey@o0.ingest.sentry.io/0"
###< sentry/sentry-symfony ###
Performance
Performance monitoring integrations to support tracing are enabled by default. To use them, update to the latest version of the SDK.
These integrations hook into critical paths of the framework and of the vendors. As a result, there may be a performance penalty. To disable tracing, please see the Integrations documentation.
If you are not using Symfony Flex, you'll also need to enable the bundle in config/bundles.php
:
config/bundles.php
<?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
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
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
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:
<?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.
- Package:
- composer:sentry/sentry-symfony
- Version:
- 5.0.1
- Repository:
- https://github.com/getsentry/sentry-symfony