Integrating Rollbar with Lumen

There is a Laravel package for Rollbar (which is awesome btw), but sadly, it doesn't work in Lumen. You have to create your own service provider for it to work.

Create a new file under app/Providers called RollbarLumenServiceProvider.php and copy the following code in:

<?php

namespace App\Providers;

use Jenssegers\Rollbar\RollbarLogHandler;
use Illuminate\Support\ServiceProvider;
use InvalidArgumentException;
use Monolog\Handler\RollbarHandler;
use Rollbar;
use RollbarNotifier;

class RollbarLumenServiceProvider extends ServiceProvider
{
/**
 * Indicates if loading of the provider is deferred.
 *
 * @var bool
 */
protected $defer = false;

/**
 * Register the service provider.
 */
public function register()
{
    $this->app->configure('rollbar');

    // Don't register rollbar if it is not configured.
    if (! getenv('ROLLBAR_TOKEN') and ! $this->app['config']->get('rollbar')) {
        return;
    }

    $app = $this->app;

    $app[RollbarNotifier::class] = $app->share(function ($app) {

        // Default configuration.
        $defaults = [
            'environment'  => $app->environment(),
            'root'         => base_path(),
        ];

        $config = array_merge($defaults, $app['config']->get('services.rollbar', []));

        $config['access_token'] = getenv('ROLLBAR_TOKEN') ?: $app['config']->get('services.rollbar.access_token');

        if (empty($config['access_token'])) {
            throw new InvalidArgumentException('Rollbar access token not configured');
        }

        Rollbar::$instance = $rollbar = new RollbarNotifier($config);

        return $rollbar;
    });

    $app[RollbarLogHandler::class] = $app->share(function ($app) {
        $level = getenv('ROLLBAR_LEVEL') ?: $app['config']->get('services.rollbar.level', 'debug');

        $handler = app(RollbarHandler::class, [$this->app[RollbarNotifier::class], $level]);

        return $handler;
    });

    // Register the fatal error handler.
    register_shutdown_function(function () use ($app) {
        if (isset($app[Rollbar::class])) {
            $app->make(Rollbar::class);
            Rollbar::report_fatal_error();
        }
    });

    // If the Rollbar client was resolved, then there is a possibility that there
    // are unsent error messages in the internal queue, so let's flush them.
    register_shutdown_function(function () use ($app) {
        if (isset($app[Rollbar::class])) {
            $app[Rollbar::class]->flush();
        }
    });
}

public function boot()
{
    $app = $this->app;

    // Listen to log messages.
    $app['log']->pushHandler(
        app(RollbarLogHandler::class, [
            $this->app[Rollbar::class]
        ])
    );
}

public function provides()
{
    return [
        RollbarLogHandler::class
    ];
}
}

Then, open bootstrap/app.php and register your provider $app->register(\App\Providers\RollbarLumenServiceProvider::class);

or, if you just want it to report production errors (recommended)

if ($app->environment() === 'production') { $app->register(\App\Providers\RollbarLumenServiceProvider::class); }

While your there, tell Lumen to configure your rollbar config $app->configure('rollbar');. You'll need to put this above the service provider.

Now, add a new folder called config under your root and add a new file called rollbar.php (/your-lumen-root/config/rollbar.php)

<?php

return [
    'access_token' => env('ROLLBAR_TOKEN'),
    'level' => env('ROLLBAR_LEVEL'),
];

Now, add the your rollbar token and rollbar level in you .env file and you're good to start reporting errors automatically to Rollbar!