Simplify Laravel Error Logging with PHP Rollbar Integration

 Simplify Laravel Error Logging with PHP Rollbar Integration


Developers always try to come up with a clean code and simple logical structure. This means building applications with easy-to-understand backend code which gives zero downtime. They always try to ensure glitch-free app execution through writing several tests to check run-time performance on various Laravel error logging platforms, browsers, and devices etc.
But testing every single line of code and functional operation is quite difficult, or you can say it’s close to impossible. The runtime errors and exceptions cause a nightmare for the developers, and most of this happens in production environment.
As a developer, I always get stuck with broken exceptions and fatal errors, no matter how much I try to write clean and structured code. These weird bugs always emerge on the server and it takes a lot of time to fix back properly.
What if we have a tool that can let us know about the error with an alert in real time? A tool that can give run-time Laravel error logging and notifications about the uncaught exception before it breaks the code execution. Having a tool like this will help us fix bugs quickly, which will eventually increase our productivity to a much greater extent.
Fortunately, we have a tool called Rollbar to handle all these complicated operations. It really helps in finding various in-depth bugs and eases code debugging process in an efficient way.
This article demonstrates the integration of Rollbar into Laravel and its complete usage process to make the application code error-free.

Prerequisites

For the purpose of this tutorial, I will use a Laravel application installed on a web server. My setup is:
  • Laravel 5.5
  • PHP 7.1
  • MySQL

What exactly is Rollbar?

Rollbar is a unique monitoring platform that makes it easy to find errors and fix bugs at runtime. It works to ease out Laravel error tracking and code debugging effectively. It examines the client and server ends of an application and looks for collective errors that arise during the production phase. Finding them, it reports these bugs to the developers asking them to stop / terminate the service immediately.
Once the app counters any error, Rollbar quickly collects the information and reports to the public API. The app also analyzes and sends alerts to the developers in real time, enabling them to fix the fatal errors in time.

What are the benefits of Rollbar?

Rollbar is not only used for Laravel error logging and detection of potential errors, but also gives clear insights of the codebase and complete app analytics. It has a unique interface to search for errors and exceptions that helps developers understand bugs and apply quick fixes.
Using Rollbar in PHP, you can find core of specific errors as well as issues related to the associated processes. Moreover, it is easy to integrate into the application and gives developers complete acquaintance of the app insights. 

Key Features of Rollbar

1. Error Monitoring and Reporting

Rollbar’s main objective is to provide full stack Laravel error tracking. It comes extremely handy for developers in finding bugs and providing timely fixes to make the app execution glitch-free.
Rollbar also allows easy third-party module integration into the development stack, having PHP on the backend and JavaScript on the frontend.
Hence it monitors the entire software application and notifies about the possible errors, exceptions and unseen bugs. The most significant part is its fast alerting system which initiates as soon as the error occurs.
Another important part of Rollbar in PHP is that it makes error reporting easy to understand and developer-friendly. It does not only report errors, but also gives complete insights of severity of errors that occur spontaneously. This helps developers choose which error requires immediate attention and what steps are needed to resolve it.

2. Error queuing, deduping and grouping

Rollbar dedupes errors and makes developers’ job easy to debug similar class of errors. Deduplication is a technique which eliminates duplicate copies of repeating data. For instance, if similar error repeats itself twenty times, then through Rollbar’s deduplication technique it only becomes one error class. Hence notifying developers to take care of that singular error in order to rectify the remaining repetitive ones.
It also allows grouping of errors, giving developers the flexibility to choose which error group to resolve first based on its complexity.

3. Error Analytics

RQL (Rollbar Query Language) enables developers to use routine SQL statements to query the database having Rollbar stored errors. It provides a dashboard with a interactive analytics interface design that is quite easy to use and understand. Moreover, there isn’t any limitation to learn the specific core of those errors and why they occur.

Getting Started with Rollbar

Setup Rollbar

To use Rollbar in PHP, first register an account there. It offers different plans based on the product type and size of the team using the platform.
For the purpose of this Laravel Rollbar tutorial, I have selected the free plan. Meanwhile the account registration on Rollbar’s official site is quite a straightforward process. Just enter the email and desired password and you are good to go.
Once signed in, you will see a dashboard with various options. Simply click the “Create a new project” button and a form will show up. It will ask some initial information about the project which you have to fill very carefully. Once you are done, submit it and get the access tokens.
Next, you will get the option to choose desired SDK based on the backend of your project. From Laravel to JavaScript, Ruby to Python and many others, there are multiple options available to choose from and configure Rollbar integration in your project.
The access tokens are one of the most important components of Rollbar integration process. Using these access tokens, you program to let them know how to send reports to the Rollbar API.
Simply copy the access token named as “server-side access token” and follow the instructions mentioned below

Setting up Laravel with Rollbar

Once you have setup the access token successfully, the next step is to install Rollbar using the composer. Let’s open the SSH terminal and paste the following code:
  1. composer require rollbar/rollbar-laravel 2.*
After downloading and installing the dependency with composer, now just add the following code to the index file in the root directory of the project:

Add Service Providers

Now add service provider inside the ‘providers’ array in config/app.php file.
  1. <?php
  2. Rollbar\Laravel\RollbarServiceProvider::class,
If you want to load SDK conditionally instead of adding service provider in providers array, just load it in your AppServiceProvider as shown below:
  1. lass AppServiceProvider extends ServiceProvider
  2. {
  3. public function register()
  4. {
  5. if ($this->app->environment(['staging', 'production'])) {
  6. $this->app->register(\Rollbar\Laravel\RollbarServiceProvider::class);
  7. }
  8. }
  9. }

View

  1. <?php
  2. require_once 'vendor/autoload.php';
  3. $config = array(
  4. // required
  5. 'access_token' => 'PASTE_THE_SERVER_ACCESS_TOKEN_i_COPIED_HERE',
  6. // optional - environment name. any string will do.
  7. 'environment' => 'production'
  8. );
  9. Rollbar::init($config);
That’s all! Now, you can report all errors and uncaught exceptions in the Rollbar.

Usage

This package will automatically send every logged message to Rollbar whose level is higher than the one defined in ROLLBAR_LEVEL.

Logging a Specific Message

You can also show your own messages on specific events in the app. For example, log a debug message as:
  1. <?php
  2. \Log::debug('Here is some debug information');
  3. Adding Context Information
  4. You can pass user information as context like this:
  5. <?php
  6. \Log::error('Something went wrong', [
  7. 'person' => ['id' =>(string) 1583, 'username' => 'Pardeep', 'email' =>’pardeep@kumar.com']
  8. ]);
You can also receive emails on your given email address as:

Final Words

This brings us to the end of this article. In it, I have demonstrated in detail how to get Laravel error logging reporting through Rollbar integration. As defined above, using Rollbar in PHP apps makes it easy for developers to find uncaught errors through real time reporting. If you still have more questions regarding Rollbar or its integration in Laravel, you can write your comments below, and I will get back to you.

Comments