Symfony
Rollbar SDK for Symfony | Support Level: Not Supported
This plugin integrates Rollbar into your Symfony installation.
Requirements
This bundle depends on symfony/monolog-bundle.
Installation
Symfony 4 and later
- Add
Rollbar for Symfonywith composer:composer require rollbar/rollbar-php-symfony-bundle. - Register
Rollbar\Symfony\RollbarBundle\RollbarBundleinconfig/bundles.phpby adding the following entry at the end of your bundle array:
<?php
Rollbar\Symfony\RollbarBundle\RollbarBundle::class => ['all' => true]
?>
- Add RollbarHandler for appropriate environments in
config/packages/dev/monolog.yaml,config/packages/test/monolog.yamlandconfig/packages/prod/monolog.yamlby adding the following in thehandlerssection:
rollbar:
type: service
id: Rollbar\Monolog\Handler\RollbarHandler
- Configure your Rollbar setup in
config/packages/rollbar.yamlor in any of the environment subdirectories (i.e.,config/packages/prod/rollbar.yaml:
rollbar:
enabled: true
access_token: [your access token]
environment: [environment name]
[other Rollbar config options]
Symfony 3
- Add
Rollbar for Symfonywith composer:composer require rollbar/rollbar-php-symfony-bundle - Register
Rollbar\Symfony\RollbarBundle\RollbarBundleinAppKernel::registerBundles()after registering theMonologBundle(new Symfony\Bundle\MonologBundle\MonologBundle()).
public function registerBundles()
{
$bundles = [
// ...
new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
// ...
new Symfony\Bundle\MonologBundle\MonologBundle(),
// ...
new Rollbar\Symfony\RollbarBundle\RollbarBundle(),
// ...
];
return $bundles;
}
- Configure Rollbar and Monolog in your
app/config/config.ymlorapp/config/config_*.yml.
rollbar:
access_token: YourAccessToken
environment: YourEnvironmentName
monolog:
handlers:
rollbar:
type: service
id: Rollbar\Monolog\Handler\RollbarHandler
Usage
Exception reporting
Exceptions will be reported to Rollbar automatically after you install and configure the bundle.
Manual reporting
This bundle injects itself into the Monolog loggers. Thanks to this, all of the Monolog logs will also be automatically passed to Rollbar.
All you need to do is obtain the LoggerInterface implementation from the service container.
<?php
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Psr\Log\LoggerInterface;
class DefaultController extends Controller
{
/**
* @Route("/", name="homepage")
*/
public function indexAction(Request $request, LoggerInterface $logger)
{
$logger->error('Test info with person data');
// replace this example code with whatever you need
return $this->render('default/index.html.twig', [
'base_dir' => realpath($this->getParameter('kernel.project_dir')).DIRECTORY_SEPARATOR,
]);
}
}
Configuration
You can see the Rollbar configuration options here.
All of them can be configured by nesting them in the rollbar array, i.e.:
rollbar:
access_token: YourAccessToken
environment: YourEnvironmentName
scrub_fields: [password, password_confirmation, credit_card_number]
person configuration option
person configuration optionBy default, this bundle fetches the user data with $container->get('security.token_storage')->getToken()->getUser(). However, you can hardcode your own person data here. Although this might be used rarely, if you want to pass user data to Rollbar, you probably want to set up person_fn (see below).
person_fn configuration option
person_fn configuration optionNote: data returned by the person_fn callable will overwrite any data provided in person config or fetched from Symfony's $container->get('security.token_storage')->getToken()->getUser().
You can provide your own logic for retrieving user data with the person_fn configuration option. The value should be a PHP callable returning an array of data in the person format, i.e.:
rollbar:
access_token: YourAccessToken
environment: YourEnvironmentName
person_fn: '\Example\UserData::personFn'
<?php
namespace Example;
class UserData
{
public static function personFn()
{
return array(
'id' => '444'
);
}
}
Help / Support
If you run into any issues, please email us at [email protected]
For bug reports, please open an issue on GitHub.
For more information on rollbar-php, please see the docs here.
Updated about 1 year ago