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 Forge

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

Visit Laravel Forge
Laravel Forge logo

Laravel Forge

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

Laravel Forge
Tinkerwell logo

Tinkerwell

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

Tinkerwell
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 $7500/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
Shift logo

Shift

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

Shift
Bacancy logo

Bacancy

Supercharge your project with a seasoned Laravel developer with 4-6 years of experience for just $2500/month. Get 160 hours of dedicated expertise & a risk-free 15-day trial. Schedule a call now!

Bacancy
LoadForge logo

LoadForge

Easy, affordable load testing and stress tests for websites, APIs and databases.

LoadForge
Paragraph logo

Paragraph

Manage your Laravel app as if it was a CMS – edit any text on any page or in any email without touching Blade or language files.

Paragraph
Lucky Media logo

Lucky Media

Bespoke software solutions built for your business. We ♥ Laravel

Lucky Media
Lunar: Laravel E-Commerce logo

Lunar: Laravel E-Commerce

E-Commerce for Laravel. An open-source package that brings the power of modern headless e-commerce functionality to Laravel.

Lunar: Laravel E-Commerce
DocuWriter.ai logo

DocuWriter.ai

Save hours of manually writing Code Documentation, Comments & DocBlocks, Test suites and Refactoring.

DocuWriter.ai
Rector logo

Rector

Your partner for seamless Laravel upgrades, cutting costs, and accelerating innovation for successful companies

Rector

The latest

View all →
Generate Code Coverage in Laravel With PCOV image

Generate Code Coverage in Laravel With PCOV

Read article
Non-backed Enums in Database Queries and a withSchedule() bootstrap method in Laravel 11.1 image

Non-backed Enums in Database Queries and a withSchedule() bootstrap method in Laravel 11.1

Read article
Laravel Pint --bail Flag image

Laravel Pint --bail Flag

Read article
Laravel Herd for Windows is now released! image

Laravel Herd for Windows is now released!

Read article
The Laravel Worldwide Meetup is Today image

The Laravel Worldwide Meetup is Today

Read article
Cache Routes with Cloudflare in Laravel image

Cache Routes with Cloudflare in Laravel

Read article