Get expert guidance in a few days with a Laravel code review

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
Tinkerwell

Enjoy coding and debugging in an editor designed for fast feedback and quick iterations. It's like a shell for your application – but with multi-line editing, code completion, and more.

Visit Tinkerwell
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
PhpStorm logo

PhpStorm

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

PhpStorm
Get expert guidance in a few days with a Laravel code review logo

Get expert guidance in a few days with a Laravel code review

Expert code review! Get clear, practical feedback from two Laravel devs with 10+ years of experience helping teams build better apps.

Get expert guidance in a few days with a Laravel code review
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
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
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
Laravel Cloud logo

Laravel Cloud

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

Laravel Cloud
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
Shift logo

Shift

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

Shift

The latest

View all →
Laravel Schema Sentinel: Detect and Fix Database Schema Drift image

Laravel Schema Sentinel: Detect and Fix Database Schema Drift

Read article
Laravel Web Push Notifications image

Laravel Web Push Notifications

Read article
RedBerry to Host Georgia's First Laravel Meetup in Tbilisi image

RedBerry to Host Georgia's First Laravel Meetup in Tbilisi

Read article
Interruptible Jobs in Laravel 13.7.0 image

Interruptible Jobs in Laravel 13.7.0

Read article
A Free Shift to Check If Your App is Ready for Laravel Cloud image

A Free Shift to Check If Your App is Ready for Laravel Cloud

Read article
Laravel Idempotency: HTTP Idempotency Middleware for Laravel image

Laravel Idempotency: HTTP Idempotency Middleware for Laravel

Read article