Jens Segers on Aug 09 2013

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


Comments

[email protected] 1 year ago

Esses dois fatores me fizeram um apaixonado por lugares inusitados. Adoraria ir para França e conhecer a Torre Eiffel, mas antes disso penso em lugares como Islândia, Deserto do Saara ou algum vilarejo na Namíbia. Gosto de me sentir vivo, e é no extremo, no diferente, que isso chega até mim, e não acontece somente comigo. Converso com muita gente que se identifica com esses destinos inusitados e com todas as minhas aventuras. timberland boots for men red and white http://www.yellowtimberlandboots.com/Men's-Timberland-6-Inch-Premium-Boots.html


Bishal Paudel 4 years ago

Thank you so much. Rollbar is required for every projects.


Jens Segers 4 years ago

@potsky, I still haven't figured out which one I like more. Rollbar has better email notifications but Sentry has an open source version that you can host yourself: https://github.com/getsentry/sentry


potsky 4 years ago

Hi jens!

I have seen that you are still updating both Laravel Rollbar and Sentry (the app, not the auth package) packages on GitHub so I suppose you are still using both packages.

For now I use my own error logging system with accumulator, email and SMS notifications, stack trace, ... and I would like to switch to one of these services.

Rollbar seems to be more featured (replay, code matching with GitHub integration) but I can install and manage my own sentry instance for free.

Which one do you prefer ? Are they equivalent ? Are they both easy to use ?

Thank you for your opinion and really thank you for your great Laravel packages, especially your MangoDB one !

potsky


Jens Segers 4 years ago

@Ben; I should create a laravel-rollbar package to make this a bit easier :)


Ben Holz 4 years ago

Thanks for quick tutorial! Thought I might add that Rollbar recommends you flush the payload - here is excerpt from the documentation:

If you're using Rollbar in a long-running script, such as a Laravel project or a background worker, you may want to manually flush the batch. To flush, simply call:

Rollbar::flush(); For example, if using Laravel, add the above line to your App::after() event handler. Or in a looping background worker, call it at the end of each loop.


Sebastiaan 4 years ago

Just what I needed (really)!