Laravel Cloud is here! Zero-config managed infrastructure for Laravel apps. Deploy now.

Automated Laravel Error Logging and Monitoring

Published on by

Automated Laravel Error Logging and Monitoring image

We’re happy to introduce our latest error reporting SDK for Laravel, which tracks and reports errors that happen in your Laravel applications.
In real-world production applications, it’s important to monitor errors so you understand your user’s experience and can fix issues before more are impacted. Rollbar helps by providing you with a live error feed from your application, including complete stack traces for instant visibility. To give you more context to debug problems, we also track the environment the error is coming from (prod or staging), the server that generated the error, and even the user’s session. Learn more about Rollbar’s product features.

Below, we’ll show you how to add Rollbar error reporting to your Laravel applications and even give you a code example that you can try yourself.

Laravel error reporting with Rollbar

Here are some simple steps describing how to integrate Laravel SDK in your code. You can find more details in the Laravel Documentation.

  1. Sign up for a Rollbar account if you haven’t done so yet.
  2. Create your project and select Other from the list of notifiers. Copy the server side access token that is generated for you. You’ll need this to configure Rollbar in the steps below.
  3. Open command prompt in your project directory and type following command to install the Laravel notifier SDK.

composer require rollbar/rollbar-laravel

  1. Add the project access token from step 1 in your .env file. You can find .env file in your project directory.

ROLLBAR_TOKEN=[your Rollbar project access token]

  1. Add the Rollbar service provider in the providers array config/app.php. You can find the config directory under your project.

Rollbar\Laravel\RollbarServiceProvider::class,

  1. If you only want to enable Rollbar reporting for certain environments you can conditionally load the service provider in your AppServiceProvider. You can find it in the app/providers folder under project directory.
public function register()
{
if ($this->app->environment('production')) {
$this->app->register(\Rollbar\Laravel\RollbarServiceProvider::class);
}
}
  1. Add the following snippet in the config/services.php. You can find the config directory under the main project directory.
'rollbar' => [
'access_token' => env('ROLLBAR_TOKEN'),
'level' => env('ROLLBAR_LEVEL'),
],

The level variable defines the minimum log level at which log messages are sent to Rollbar. If not specified, the default is debug. For development, you could set this either to debug to send all log messages, or to none to send no messages at all. For production, you could set this to error or warn so that info and debug messages are ignored.

Testing Rollbar with an example Laravel application

Check out our project on GitHub called Rollbar-Laravel-Example. To run it, clone the project and replace the access token with your project access token. You can find the access token inside .env file under your project directory.

When you click the “Create an error” button it will generate an error message. It triggers the MessagesController@submit action which has mapped to the home/submit url. In the submit method of MessagesController, we attempt to access the foo field on x, which is empty. This results in the error “ErrorException: Create default object from empty value”.

<br></br><?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
 
class MessagesController extends Controller
{
public function submit(){
try{
$x = null;
$x->foo = 5; // Null field access
} catch(Exception $ex) {
echo "Caught exception";
}
return "SUCCESS";
}
}

Viewing and reporting Laravel errors in Rollbar

Next open Rollbar to see what these Laravel errors look like in your account’s error occurrences page. The error we just generated should be called “ErrorException: Create default object from empty value”.

Get more details by clicking on the error. You can now see a traceback showing you the exact source code file, method, and line number that generated the error.

For errors that impact many customers or are difficult to troubleshoot, Rollbar gives you additional context to solve them faster. With source code integration, you can click on the file to view in GitHub and see what changes were recently made. You can also see when the problem started, which deployment is suspected, and even which browsers and people are affected. I can also set alerts when the problem happens again. This will help you quickly determine the business impact, fix the problem, and proactively respond in the future.

If you haven’t already, signup for a 14-day free trial of Rollbar and let us help you take control of Laravel errors.


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

Eric L. Barnes photo

Eric is the creator of Laravel News and has been covering Laravel since 2012.

Filed in:
Cube

Laravel Newsletter

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

image
Jump24 - UK Laravel Agency

Laravel Developers that Click into Place. Never outsourced. Never offshored. Always exceptional.

Visit Jump24 - UK Laravel Agency
Tinkerwell logo

Tinkerwell

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

Tinkerwell
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
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
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
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
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
Lucky Media logo

Lucky Media

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

Lucky Media
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

The latest

View all →
Laravel Pint Now Replaces Fully Qualified Class Names with Imports image

Laravel Pint Now Replaces Fully Qualified Class Names with Imports

Read article
Inertia v3 Upgrade Prompt and JSON Log Support in Laravel Boost v2.3.0 image

Inertia v3 Upgrade Prompt and JSON Log Support in Laravel Boost v2.3.0

Read article
Laracon AU Returns to Brisbane - Call for Speakers Now Open image

Laracon AU Returns to Brisbane - Call for Speakers Now Open

Read article
Detecting and Fixing Race Conditions in Laravel Applications image

Detecting and Fixing Race Conditions in Laravel Applications

Read article
LaraCopilot: Generate Laravel MVPs From a Single Prompt With AI image

LaraCopilot: Generate Laravel MVPs From a Single Prompt With AI

Read article
Model::withoutRelation() in Laravel 12.54.0 image

Model::withoutRelation() in Laravel 12.54.0

Read article