Laravel

Rollbar SDK for Laravel

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 5.6 and up

  1. Install using composer:
composer require rollbar/rollbar-laravel
  1. Add Project Access Token post_server_item from Rollbar.com → Settings → Project Access Tokens to .env:
ROLLBAR_TOKEN=[your Rollbar project access token]
  1. Add the service provider to the 'providers' array in config/app.php:
<?php
Rollbar\Laravel\RollbarServiceProvider::class,
  1. In config/logging.php, add rollbar logging channel by adding the following under channel key:
<?php
        'rollbar' => [
            'driver' => 'monolog',
            'handler' => \Rollbar\Laravel\MonologHandler::class,
            'access_token' => env('ROLLBAR_TOKEN'),
            'level' => 'error',
        ]

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

  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
  1. Add Project Access Token post_server_item from Rollbar.com → Settings → Project Access Tokens to .env:
ROLLBAR_TOKEN=[your Rollbar project access token]
  1. Add the service provider to the 'providers' array in config/app.php (this package also supports Laravel 5.5's auto-discovery, which allows you to skip this step):
<?php
Rollbar\Laravel\RollbarServiceProvider::class,

(Optional) Disabling Laravel 5.5's auto-discovery

By default Rollbar Laravel supports auto-discovery. If you do not want to auto-discover the package add the following code to your composer.json:

"extra": {
        "laravel": {
            "dont-discover": [
                "rollbar/rollbar-laravel"
            ]
        }
    },

Don't forget to run $ php artisan package:discover to recreate Package manifest file.

Enabling / disabling Rollbar on specific environments

First, disable auto-discovery.

If you only want to enable Rollbar reporting for certain environments you can conditionally load the service provider in your AppServiceProvider:

<?php
public function register()
{
    if ($this->app->environment('production')) {
        $this->app->register(\Rollbar\Laravel\RollbarServiceProvider::class);
    }
}

Usage

This package will automatically send to Rollbar every logged message whose level is higher than the ROLLBAR_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:

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

Adding Context Informaton

You can pass user information as context like this:

<?php
\Log::error('Something went wrong', [
    'person' => ['id' =>(string) 123, 'username' => 'John Doe', 'email' => 'john@doe.com']
]);

Or pass some extra information:

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

Exception Logging

Laravel 5.6 and up

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

NOTE: This SDK overrides the default binding for the exception handler and resolved to \Rollbar\Laravel\ExceptionHandler which extends \App\Exceptions\Handler. Keep that in mind if you application also overrides the default binding for Illuminate\Contracts\Debug\ExceptionHandler.

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:

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

The level variable 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'),
],

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:

<?php
$config = array(
  // ... 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.

<?php
$config = array(
  // ... 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 support@rollbar.com

For bug reports, please open an issue on GitHub.

Laravel

Rollbar SDK for Laravel