Laravel

Rollbar SDK for Laravel | Support Level: Supported

Rollbar error monitoring integration for Laravel projects. This library adds a listener to Laravel's logging component. Laravel's session information will be sent into Rollbar, as well as some other helpful information such as environment, server, and session.

Installation

Laravel 6 through 13

  1. Install using Composer. It picks the version that matches your Laravel release: 8.x on Laravel 10 and later, 7.x on Laravel 6 to 8.
composer require rollbar/rollbar-laravel

On Laravel 9, run this instead, because the plain command installs an outdated package that cannot send errors. Use a lowercase -w, since an uppercase -W tries to update all of Laravel.

composer require rollbar/rollbar-laravel:^7.2 -w
  1. Add Project Access Token post_server_item from Rollbar.com → Settings → Project Access Tokens to .env:
ROLLBAR_TOKEN={{YOUR_POST_SERVER_ITEM_TOKEN}}
  1. In config/logging.php, add the rollbar logging channel by adding the following under the channels key:
    'rollbar' => [
        'driver' => 'monolog',
        'handler' => \Rollbar\Laravel\MonologHandler::class,
        'access_token' => env('ROLLBAR_TOKEN'),
        'level' => 'debug',
4. Pick how Laravel sends logs to Rollbar. To make Rollbar the default channel, set `LOG_CHANNEL=rollbar` in `.env`. To keep your existing logging and add Rollbar, replace the `stack` block in `config/logging.php` with:

```php PHP
'stack' => [
            'driver' => 'stack',
            'channels' => ['rollbar','single'],
            'ignore_exceptions' => false,
        ],

and keep LOG_CHANNEL=stack in .env. On Laravel 11 and later you can instead set LOG_STACK=rollbar,single in .env and leave the block alone. Editing the default key in config/logging.php has no effect on a new app, because .env sets LOG_CHANNEL.
],


5. Send a test message

```php PHP
\Log::debug('Test debug message');

To the above array you can add any other Rollbar configuration options you would normally use in the Rollbar PHP SDK.

For example, to associate an error with the user that is currently logged in, add the configuration option 'person_fn' and set it to 'Auth::user':

        'rollbar' => [
            // ... rest of current config
            'person_fn' => 'Auth::user',
            'capture_email' => true,            //optional
            'capture_username' => true     //optional
        ],

Laravel 5.6 to 5.8

  1. Install using composer:
composer require rollbar/rollbar-laravel 4.*
  1. Add Project Access Token post_server_item from Rollbar.com → Settings → Project Access Tokens to .env:
ROLLBAR_TOKEN={{YOUR_POST_SERVER_ITEM_TOKEN}}
  1. In config/logging.php, add the rollbar logging channel by adding the following under the channels key:
    'rollbar' => [
        'driver' => 'monolog',
        'handler' => \Rollbar\Laravel\MonologHandler::class,
        'access_token' => env('ROLLBAR_TOKEN'),
        'level' => 'debug',
    ],

To the above array you can add any other Rollbar configuration options you would normally use in the Rollbar PHP SDK.

For example, to associate an error with the user that is currently logged in, add the configuration option 'person_fn' and set it to 'Auth::user':

        'rollbar' => [
            // ... rest of current config
            'person_fn' => 'Auth::user',
            'capture_email' => true,            //optional
            'capture_username' => true     //optional
        ],
  1. Also in config/logging.php, either set rollbar as the default log channel by changing the value of the default key or add rollbar to the stack channel:
<?php
       'stack' => [
            'driver' => 'stack',
            'channels' => ['rollbar','single'],
        ],

Laravel 5.5 and lower

  1. Install using composer:
composer require rollbar/rollbar-laravel 2.*
  1. Add Project Access Token post_server_item from Rollbar.com → Settings → Project Access Tokens to .env:
ROLLBAR_TOKEN={{YOUR_POST_SERVER_ITEM_TOKEN}}

Send an Error and a Message

Add the following code to a file or function and execute to send an error message:


 use Rollbar\Rollbar;
 use Rollbar\Payload\Level;
    
// installs global error and exception handlers
Rollbar::init(
    array(
        'access_token' => ROLLBAR_TEST_TOKEN,
        'environment' => 'production',
        'code_version' => '1.0.0'
    )
);
    
try {
    throw new \Exception('test exception');
} catch (\Exception $e) {
    Rollbar::log(Level::ERROR, $e);
}

// Message at level 'info'
Rollbar::log(Level::INFO, 'testing info level');

// With extra custom data (3rd arg)
Rollbar::log(
    Level::INFO,
    'testing extra data',
    array("some_key" => "some value") // key-value additional data
);
        
// If you want to check if logging with Rollbar was successful
$response = Rollbar::log(Level::INFO, 'testing wasSuccessful()');
if (!$response->wasSuccessful()) {
    throw new \Exception('logging with Rollbar failed');
}

// raises an E_NOTICE which will *not* be reported by the error handler
$foo = $bar;

// will be reported by the exception handler
throw new \Exception('testing exception handler');

Usage

This package will automatically send to Rollbar every logged message whose level is at or higher than the 'level' you have configured.

Logging a Specific Message

You can log your own messages anywhere in your app. For example, to log a debug message:

\Log::debug('Here is some debug information');

Adding Context Information

You can pass user information as context like this:

\Log::error('Something went wrong', [
    'person' => ['id' => (string) 123, 'username' => 'John Doe', 'email' => '[email protected]']
]);

Or pass some extra information:

\Log::warning('Something went wrong', [
    'download_size' => 3432425235
]);

Exception Logging

Laravel 11 and later

Laravel 11 and later have no app/Exceptions/Handler.php. Ignore exceptions in bootstrap/app.php instead, inside the existing withExceptions section:

->withExceptions(function (Exceptions $exceptions): void {
    $exceptions->dontReport([
        \App\Exceptions\PaymentDeclinedException::class,
    ]);
})

Laravel 5.6 to 10

For Laravel 5.6, all errors which are normally handled by \App\Exceptions\Handler are going to be reported to Rollbar by default.

Laravel 5.5 and lower

NOTE: Fatal exceptions will always be sent to Rollbar.

Any exceptions that are not listed as $dontReport in your app/Exceptions/Handler.php or its parent will be sent to Rollbar automatically.

If you wish to override this to do more Rollbar reporting, you may do so using the Log facade in your error handler in app/Exceptions/Handler.php. For example, to log every exception add the following:

<?php
public function report(Exception $exception)
{
    \Log::error($exception);
    return parent::report($exception);
}

Laravel 4

For Laravel 4 installations, this is located in app/start/global.php:

<?php
App::error(function(Exception $exception, $code)
{
    Log::error($exception);
});

Configuration

Setting up ROLLBAR_TOKEN in .env should be enough for basic configuration.

Laravel 5.6 and up

This package supports configuration through the services configuration file located in config/logging.php. All rollbar configuration variables will be directly passed to Rollbar:

    'rollbar' => [
        'driver' => 'monolog',
        'handler' => \Rollbar\Laravel\MonologHandler::class,
        'level' => 'info',
        'access_token' => env('ROLLBAR_TOKEN'),
        'environment' => 'special-environment'
    ],

The level setting defines the minimum log level at which log messages are sent to Rollbar. If not specified, the default is debug. For development, you could set this either to debug to send all log messages or to none to send no messages at all. For production, you could set this to error so that all info and debug messages are ignored.

Laravel 5.5 and lower

This package supports configuration through the services configuration file located in config/services.php. All rollbar configuration variables will be directly passed to Rollbar:

<?php
'rollbar' => [
    'access_token' => env('ROLLBAR_TOKEN'),
    'level' => env('ROLLBAR_LEVEL'),
],

Conditional loading

If you wish to load the SDK conditionally:

  1. Add the dont-discover clause in your app's composer.json:
    "extra": {
        "laravel": {
            "dont-discover": [
                "rollbar/rollbar-laravel"
            ]
        }
    },
  1. Laravel normally registers the service provider automatically. With discovery turned off, register it yourself in your AppServiceProvider:
class AppServiceProvider extends ServiceProvider
{
    public function register()
    {
        if ($this->app->environment(['staging', 'production'])) {
             $this->app->register(\Rollbar\Laravel\RollbarServiceProvider::class);
        }
    }
}

Asynchronous Reporting

By default, payloads (batched or not) are sent as part of script execution. This is easy to configure but may negatively impact performance. With some additional setup, payloads can be written to a local relay file instead; that file will be consumed by rollbar-agent asynchronously. To turn this on, set the following config params:

$config = [
  // ... rest of current config
  'handler' => 'agent',
  'agent_log_location' => '/var/www'  // not including final slash. must be writeable by the user php runs as.
];

You'll also need to run the agent. See the rollbar-agent docs for setup instructions.

Centralized Log Aggregation with fluentd

If you have a fluentd instance running available you can forward payloads to this instance. To turn this on, set the following config params.

$config = [
  // ... rest of current config
  'handler' => 'fluent',
  'fluent_host' => 'localhost',  // localhost is the default setting but any other host IP or a unix socket is possible
  'fluent_port' => 24224, // 24224 is the default setting, please adapt it to your settings
  'fluent_tag' => 'rollbar', // rollbar is the default setting, you can adjust it to your needs
];

Also, you will have to install a suggested package fluent/logger.

Configuration Reference

See the Rollbar-PHP Configuration Reference

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.


Did this page help you?