Jens Segers on

Integrating Rollbar in Laravel

Recently I discovered rollbar.com, which is a remote error logging system that allows you to collect errors and logs from multiple applications into a single dashboard. It will send you notifications if certain errors occur on specific environment and has support for multiple programming languages such as Ruby, Python, PHP, Node.js, Javascript and Flash. I already integrated Rollbar into some of my Laravel 4 projects and I will show you how I did it.

rollbar

First you need to install their official composer package by adding the following to your composer.json file:

"rollbar/rollbar": "dev-master",

After installing the package with the composer update command, you need to plug their small library into the existing Laravel 4 error handler. This is done by modifying the basic error handler in the app/start/global.php file:

App::error(function(Exception $exception, $code)
{
    $config = array(
        'access_token' => 'your-post-server-token',
        'environment' => App::environment(),
        'root' => base_path(),
        'max_errno' => E_USER_NOTICE
    );

    Rollbar::init($config, false, false);
    Rollbar::report_exception($exception);
});

The difference between this code and the code from the Rollbar tutorial is to disable the PHP error and exception handler registration by the Rollbar library because Laravel is taking care of that.

You can also repeat the code in a log listener if you want your log messages to be sent to Rollbar as well:

Log::listen(function($level, $message, $context)
{
    $config = array(
        'access_token' => 'your-post-server-token',
        'environment' => App::environment(),
        'root' => base_path(),
        'max_errno' => E_USER_NOTICE
    );

    Rollbar::init($config, false, false);
    Rollbar::report_message($message, $level, $context);
});

Update: I created a Laravel package that will make this integration even easier: https://github.com/jenssegers/laravel-rollbar

Webmentions

Tweet about this blog post and you will appear below!