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
Larafast: Laravel SaaS Starter Kit

Larafast is a Laravel SaaS Starter Kit with ready-to-go features for Payments, Auth, Admin, Blog, SEO, and beautiful themes.

Visit Larafast: Laravel SaaS Starter Kit
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
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
LaraJobs logo

LaraJobs

The official Laravel job board

LaraJobs
Larafast: Laravel SaaS Starter Kit logo

Larafast: Laravel SaaS Starter Kit

Larafast is a Laravel SaaS Starter Kit with ready-to-go features for Payments, Auth, Admin, Blog, SEO, and beautiful themes. Available with VILT and TALL stacks.

Larafast: Laravel SaaS Starter Kit
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a 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
Rector logo

Rector

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

Rector

The latest

View all →
Apply Dynamic Filters to Eloquent Models with the Filterable Package image

Apply Dynamic Filters to Eloquent Models with the Filterable Package

Read article
Property Hooks Get Closer to Becoming a Reality in PHP 8.4 image

Property Hooks Get Closer to Becoming a Reality in PHP 8.4

Read article
Asserting Exceptions in Laravel Tests image

Asserting Exceptions in Laravel Tests

Read article
Reversible Form Prompts and a New Exceptions Facade in Laravel 11.4 image

Reversible Form Prompts and a New Exceptions Facade in Laravel 11.4

Read article
Basset is an alternative way to load CSS & JS assets image

Basset is an alternative way to load CSS & JS assets

Read article
Integrate Laravel with Stripe Connect Using This Package image

Integrate Laravel with Stripe Connect Using This Package

Read article