Rollbar Config for your Laravel App – Sponsor

Published on by

Rollbar Config for your Laravel App – Sponsor image

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.

Cameron Scott photo

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

Filed in:
Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.

image
Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Visit Laravel Cloud
PhpStorm logo

PhpStorm

The go-to PHP IDE with extensive out-of-the-box support for Laravel and its ecosystem.

PhpStorm
Laravel Cloud logo

Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Cloud
Harpoon: Next generation time tracking and invoicing logo

Harpoon: Next generation time tracking and invoicing

The next generation time-tracking and billing software that helps your agency plan and forecast a profitable future.

Harpoon: Next generation time tracking and invoicing
Acquaint Softtech logo

Acquaint Softtech

Acquaint Softtech offers AI-ready Laravel developers who onboard in 48 hours at $3000/Month with no lengthy sales process and a 100 percent money-back guarantee.

Acquaint Softtech
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

Lucky Media
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit
No Compromises logo

No Compromises

Joel and Aaron, the two seasoned devs from the No Compromises podcast, are now available to hire for your Laravel project. ⬧ Flat rate of $9500/mo. ⬧ No lengthy sales process. ⬧ No contracts. ⬧ 100% money back guarantee.

No Compromises
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum

The latest

View all →
A Copy/Paste Detector CLI for PHP 8.5+ image

A Copy/Paste Detector CLI for PHP 8.5+

Read article
Commune: A Private Community for Founders and Builders image

Commune: A Private Community for Founders and Builders

Read article
 Laravel AI Tasks: An AI Orchestration Package for Queues, Logging, and Cost Control image

Laravel AI Tasks: An AI Orchestration Package for Queues, Logging, and Cost Control

Read article
Worker Metrics on the WorkerStopping Event in Laravel 13.18 image

Worker Metrics on the WorkerStopping Event in Laravel 13.18

Read article
Clonio CLI: Clone Production Databases With Anonymized Data image

Clonio CLI: Clone Production Databases With Anonymized Data

Read article
Ship AI with Laravel: Test Your AI System with Zero API Calls image

Ship AI with Laravel: Test Your AI System with Zero API Calls

Read article