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 debug PHP errors quickly, which will eventually increase our productivity to a much greater extent.
Stop Wasting Time on Servers
Cloudways handle server management for you so you can focus on creating great apps and keeping your clients happy.
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
I have used Laravel application on a Cloudways managed Laravel server because it gives a highly optimized hosting stack and takes care of any server level issue. I can sign up for a free account.
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.
You Might Also Like: Use Laravel Unit Testing to Avoid Project-Wrecking Mistakes
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.
You Might Also Like: PHP Unit Testing Using PHPUnit Framework
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 for Error Logging in Laravel
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
Improve Your Laravel App Speed by 300%
Cloudways offers you dedicated servers with SSD storage, custom performance, an optimized stack, and more for 300% faster load times.
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:
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.
<?php 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:
lass AppServiceProvider extends ServiceProvider { public function register() { if ($this->app->environment(['staging', 'production'])) { $this->app->register(\Rollbar\Laravel\RollbarServiceProvider::class); } } }
View
<?php require_once 'vendor/autoload.php'; $config = array( // required 'access_token' => 'PASTE_THE_SERVER_ACCESS_TOKEN_i_COPIED_HERE', // optional - environment name. any string will do. 'environment' => 'production' ); 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:
<?php \Log::debug('Here is some debug information'); Adding Context Information You can pass user information as context like this: <?php \Log::error('Something went wrong', [ 'person' => ['id' =>(string) 1583, 'username' => 'Pardeep', 'email' =>’[email protected]'] ]);
You can also receive emails on your given email address as:
Q: How to resolve the Laravel error in exception handler?
A: To resolve the Laravel error in exception handler, try enabling debug from /app/config/app.php directory. Here’s how to do this quickly:
1. Go to /app/config/app.php
2. Change ‘debug’ => false to true
After enabling debug as shown above, access the web page again and you won’t the “error in exception handler” again.
Q: What is Laravel error handling?
A: To handle errors and exceptions in Laravel, the framework provides a pre-configured error handling class called Handler. Inside the App/Exceptions/Handler class, all the exceptions and errors that could arise in a Laravel project are defined.This Laravel error handling class contains two methods i.e Render and report, both are used to handle exceptions and errors in Laravel.
Q: How to log exceptions in Laravel?
A: In Laravel, all the exceptions are logged inside storage/logs folder. This Laravel log exception folder keeps track of all the routine exceptions that triggers during the app execution. While if you don’t want to report any particular exception, you can do that also updating the $dontReport property inside the App\Exceptions\Handler directory.
Q: What is Laravel exception handling?
A: Laravel makes exception handling quite easy. The framework comes pre-built with an exception handler class that makes easy to report and render app exceptions. All the exceptions are reported inside the app/Exceptions/Handler.php file. The handler class is used to report and render the exceptions via same report & render method. The report method logs the errors inside the PHP error logging file, while the render method is used to customize a response for different types of exceptions.
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.
Shahzeb Ahmed
Shahzeb is a Digital Marketer with a Software Engineering background, works as a Community Manager — PHP Community at Cloudways. He is growth ambitious and aims to learn & share information about PHP & Laravel Development through practice and experimentation. He loves to travel and explore new ideas whenever he finds time. Get in touch with him at [email protected]