Published on May 8, 2016 | Posted in Laravel
Using Rollbar with Laravel and Laravel Envoyer
I stumbled across a great application called Rollbar which logs application error in a variety of languages such as Php, Perl, Javascript and Ruby to name but a few. It also has integration with Trello, Slack and Github.
When i’m writing small Laravel applications. Whenever an error occured in my application I used to ssh into the error logs and figure out what was the problem. It’s not ideal but my Laravel sites are small personal projects. So I could never justify getting an error logging service.
I came across Rollbar on one of the podcasts I listen to it has loads of features and as a free tier for small appliactions which is perfect for me. I want go through the process of integrating it into a Laravel project and setting it up with Laravel Envoyer.
I’m going to assume you currently have a Rollbar account they have a free tier with no credit card needed for signup and it’s straightforward to do so, I’m also going to assume you have a Laravel application with Envoyer for Deployments. But Envoyer isn’t essential as this is only for deployment monitoring.
-
Rollbar
-
On the Dashboard click on Integrate a Notifier. Now from the list of Languages select PHP. On this page make a copy of the server-side key.
-
Laravel
-
Go to composer.json file for your application and add the following package “jenssegers/rollbar”: “^1.5” under require. This is great package which makes the Rollbar integration straightforward.
-
With the package added now go to the command line for you application do a composer update.
-
Now open up the following file config/app.php. We need to add the rollbar package as an application service provider. In providers array add in the following snippet.
Jenssegers\Rollbar\RollbarServiceProvider::class,
-
We now need to change how the existing errors are getting logged this is done in the following file app/Exceptions/Handler.php. In this file go to the report function and replace the code with the following below.
public function report(Exception $e) { \Log::error($e); return parent::report($e); }
-
In the config/services.php add in the information for rollbar.
'rollbar' => [ 'access_token' => env('ROLLBAR_TOKEN'), 'level' => env('ROLLBAR_LEVEL'), ],
-
If you want error logging in development open up the .env file in the root of your application. Copy the following into the file.
ROLLBAR_TOKEN=PASTE-YOUR-SERVER-SIDE-KEY ROLLBAR_LEVEL=debug,
Rollbar is now recording your errors. Time to integrate it into Envoyer.
-
Envoyer
-
Log into Envoyer and goto to your project. Click on the server tab then click on Manage Environment. Add in the following to your live .env file.
ROLLBAR_TOKEN=PASTE-YOUR-SERVER-SIDE-KEY ROLLBAR_LEVEL=debug,
Rollbar is now set up on live. The final bit which we need to do is set up the deployment tracking.
-
Click On the Deployment Hooks and select Purge Old Releases options. Add a new hook after this action. Then place in the following code.
curl https://api.rollbar.com/api/1/deploy/ \ -F access_token=PASTE-YOUR-SERVER-SIDE-KEY \ -F environment=production \ -F revision={{sha}} \ -F local_username=`whoami`
You should be set up for error reporting and deployment notification. I recommend spending some time going through the other integration service such as Trello and Slack.
Envoyer has made front-end devs life much easier. I too use cw+envoyer combination to deploy my PHP apps (https://www.cloudways.com/blog/php-laravel-envoyer-deployment/ ) in less time than before. Envoyer is a nice addition to workflow of today’s devs.