Symfony integration for Sentry.
Open a command console, enter your project directory and execute the following command to download the latest stable version of this bundle:
$ composer require sentry/sentry-symfony
This command requires you to have Composer installed globally, as explained in the installation chapter of the Composer documentation.
Then, enable the bundle by adding it to the list of registered bundles
in the app/AppKernel.php
file of your project:
<?php
// app/AppKernel.php
// ...
class AppKernel extends Kernel
{
public function registerBundles()
{
$bundles = array(
// ...
new Sentry\SentryBundle\SentryBundle(),
);
// ...
}
// ...
}
Add your DSN to app/config/config.yml
:
sentry:
dsn: "https://public:[email protected]/1"
The following can be configured via app/config/config.yml
:
The base path to your application. Used to trim prefixes and mark frames as part of your application.
sentry:
app_path: "/path/to/myapp"
Sentry DSN value of your project. Leaving this value empty will effectively disable Sentry reporting.
sentry:
dsn: "https://public:[email protected]/1"
The environment your code is running in (e.g. production).
sentry:
environment: "%kernel.environment%"
The version of your application. Often this is the git sha.
sentry:
release: "beeee2a06521a60e646bbb8fe38702e61e4929bf"
A list of prefixes to strip from filenames. Often these would be vendor/include paths.
sentry:
prefixes:
- /usr/lib/include
sentry:
skip_capture:
- "Symfony\\Component\\HttpKernel\\Exception\\HttpExceptionInterface"
Define which error types should be reported.
sentry:
error_types: E_ALL & ~E_DEPRECATED & ~E_NOTICE
It is possible to customize the configuration of the user context, as well
as modify the client immediately before an exception is captured by wiring
up an event subscriber to the events that are emitted by the default
configured ExceptionListener
(alternatively, you can also just defined
your own custom exception listener).
You can always replace the default ExceptionListener
with your own custom
listener. To do this, assign a different class to the exception_listener
property in your Sentry configuration, e.g.:
sentry:
exception_listener: AppBundle\EventListener\MySentryExceptionListener
... and then define the custom ExceptionListener
, e.g.:
// src/AppBundle/EventSubscriber/MySentryEventListener.php
namespace AppBundle\EventSubscriber;
use Sentry\SentryBundle\EventListener\SentryExceptionListenerInterface;
use Symfony\Component\Console\Event\ConsoleExceptionEvent;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
class MySentryExceptionListener implements SentryExceptionListenerInterface
{
// ...
public function __construct(TokenStorageInterface $tokenStorage = null, AuthorizationCheckerInterface $authorizationChecker = null, \Raven_Client $client = null, array $skipCapture, EventDispatcherInterface $dispatcher = null)
{
// ...
}
public function onKernelRequest(GetResponseEvent $event)
{
// ...
}
public function onKernelException(GetResponseForExceptionEvent $event)
{
// ...
}
public function onConsoleException(ConsoleExceptionEvent $event)
{
// ...
}
}
As a side note, while the above demonstrates a custom exception listener that
does not extend anything you could choose to extend the default
ExceptionListener
and only override the functionality that you want to.
Create a new class, e.g. MySentryEventSubscriber
:
// src/AppBundle/EventSubscriber/MySentryEventListener.php
namespace AppBundle\EventSubscriber;
use Sentry\SentryBundle\Event\SentryUserContextEvent;
use Sentry\SentryBundle\SentrySymfonyEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class MySentryEventSubscriber implements EventSubscriberInterface
{
/** @var \Raven_Client */
protected $client;
public function __construct(\Raven_Client $client)
{
$this->client = $client;
}
public static function getSubscribedEvents()
{
// return the subscribed events, their methods and priorities
return array(
SentrySymfonyEvents::PRE_CAPTURE => 'onPreCapture',
SentrySymfonyEvents::SET_USER_CONTEXT => 'onSetUserContext'
);
}
public function onSetUserContext(SentryUserContextEvent $event)
{
// ...
}
public function onPreCapture(Event $event)
{
if ($event instanceof GetResponseForExceptionEvent) {
// ...
}
elseif ($event instanceof ConsoleExceptionEvent) {
// ...
}
}
}
In the example above, if you subscribe to the PRE_CAPTURE
event you may
get an event object that caters more toward a response to a web request (e.g.
GetResponseForExceptionEvent
) or one for actions taken at the command line
(e.g. ConsoleExceptionEvent
). Depending on what and how the code was
invoked, and whether or not you need to distinguish between these events
during pre-capture, it might be best to test for the type of the event (as is
demonstrated above) before you do any relevant processing of the object.
To configure the above add the following configuration to your services definitions:
app.my_sentry_event_subscriber:
class: AppBundle\EventSubscriber\MySentryEventSubscriber
arguments:
- '@sentry.client'
tags:
- { name: kernel.event_subscriber }