Rollbar Config for your Laravel App – Sponsor


June 12th, 2017

Rollbar Config for your Laravel App – Sponsor

Rollbar is an excellent error-monitoring service that I often reach for two reasons:

They have a very generous free tier of up to 5,000 events (errors) per month, which is ideal for newer companies and smaller projects.

They allow you to pass user data with your errors, meaning you not only know when and where an error happened but—just as importantly—who it happened to.

Rollbar just upgraded their PHP SDK to version 1.0, incorporating namespacing, installation via Composer, and several other features that folks have come to expect from modern PHP software. You could use this new SDK directly with Laravel, but Rollbar itself will refer you to an excellent, easy-to-use—though somewhat sporadically maintained—third-party wrapper.

The wrapper hooks right into Laravel’s existing error handling, but despite what the documentation says, you can actually spin up basic error-tracking simply by installing the package and adding your Rollbar API credentials. However, to take full advantage of Rollbar’s user data collection, you do need to add a few lines of code to the report() function within your App\Exceptions\Handler class:

use Auth;
function report(Exception $exception)
if (Auth::check() && $this->shouldReport($exception) ) { // Confirm the authenticated user, and that the errors should be reported.
$user = Auth::User(); // Store the user in a variable (I think it reads more cleanly)
\Log::error($exception->getMessage(), [
'person' => ['id' => $user->id, 'username' => $user->first_name . ' ' . $user->last_name, 'email' => $user->email] // Pass the exception message and user-specific data.
parent::report($exception); // If no authenticated user, let Laravel report the exception anonymously as usual.

The first thing you need to do is confirm an authenticated user—if you try to pass user data without one, your app will throw a browser-based 500 error, and provide no feedback on what happened. Don’t forget to import your Auth facade, too!

Next, you need to double-check whether the exception should be reported at all. This function is extended from the parent Illuminate\Foundation\Exceptions\Handler class and checks that the exception cannot be found within the $dontReport array at the top of your App\Exceptions\Handler class. Without this, your app will start logging every error—including, for instance, when a form submission fails a validation check. That’s a surefire way to hit your 5,000 errors per month.

Then, with your authenticated user and reportable exception, you’re ready to pass our user’s info over to Rollbar. Rollbar requires, at a minimum, an ID, but also accepts an optional username and email values. In the example above, I’ve chosen to pass a concatenation of first and last name as my username, but as long as you’re passing a string, you do you.

Finally, if one of those two conditions fails, you can bail, and Laravel will handle the exception as normal.

Again, this is a great package and I—along with 385,811 other developers—am extremely grateful to the author for creating it. I hope these small tweaks will help you get greater utility both from the package—and from Rollbar itself.

Many thanks to Rollbar for sponsoring Laravel News this week through Syndicate Ads.

Filed in:

Cameron Scott

Full stack dev, product manager, releaser of hounds. Co-founder of @inquirico.