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
Add your DSN to your .env
file:
.env
Copied
###> sentry/sentry-symfony ###
SENTRY_DSN="https://examplePublicKey@o0.ingest.sentry.io/0"
###< sentry/sentry-symfony ###
In order to receive stack trace arguments in your errors, make sure to set zend.exception_ignore_args: Off
in your php.ini
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
fill_extra_context: true # Enables sending monolog context to Sentry
process_psr_3_messages: false # Disables the resolution of PSR-3 placeholders in reported messages
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").
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").