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.

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
Laravel Idea for PhpStorm logo

Laravel Idea for PhpStorm

Ultimate PhpStorm plugin for Laravel developers, delivering lightning-fast code completion, intelligent navigation, and powerful generation tools to supercharge productivity.

Laravel Idea for PhpStorm
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

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

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
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
Supercharge Your SaaS Development with FilamentFlow: The Ultimate Laravel Filament Boilerplate logo

Supercharge Your SaaS Development with FilamentFlow: The Ultimate Laravel Filament Boilerplate

Build your SaaS application in hours. Out-of-the-box multi-tenancy and seamless Stripe integration. Supports subscriptions and one-time purchases, allowing you to focus on building and creating without repetitive setup tasks.

Supercharge Your SaaS Development with FilamentFlow: The Ultimate Laravel Filament Boilerplate
JetShip - Laravel Starter Kit logo

JetShip - Laravel Starter Kit

A Laravel SaaS Boilerplate and a starter kit built on the TALL stack. It includes authentication, payments, admin panels, and more. Launch scalable apps fast with clean code, seamless deployment, and custom branding.

JetShip - Laravel Starter Kit
Rector logo

Rector

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

Rector
MongoDB logo

MongoDB

Enhance your PHP applications with the powerful integration of MongoDB and Laravel, empowering developers to build applications with ease and efficiency. Support transactional, search, analytics and mobile use cases while using the familiar Eloquent APIs. Discover how MongoDB's flexible, modern database can transform your Laravel applications.

MongoDB

The latest

View all →
URI Parsing and Mutation in Laravel 11.35 image

URI Parsing and Mutation in Laravel 11.35

Read article
Securing Laravel Sessions with ID Regeneration image

Securing Laravel Sessions with ID Regeneration

Read article
Paginate Multiple Eloquent Models with the Union Paginator Package image

Paginate Multiple Eloquent Models with the Union Paginator Package

Read article
Whisper.php - Automatic speech recognition and transcription image

Whisper.php - Automatic speech recognition and transcription

Read article
Multiple Column Plucking in Laravel Collections image

Multiple Column Plucking in Laravel Collections

Read article
File Manager Package for Livewire image

File Manager Package for Livewire

Read article