Last Updated: February 25, 2016
·
2.149K
· cossou

Catch errors in your L4 application

Hey guys! My name is Hélder and I'm writing from Lisbon, Portugal.

In my latest project divide.pt I was not sure how to handle the Exceptions (that will eventually occur) on the production server, without having to SSH and manually check the logs. So I came across Airbrake.io, a web app to collect errors from your app and organize them.

Moving on… In this short protip I will cover how you can control your app errors when it is in production.

For that we will use the awesome web app called: Airbrake.io.
A short description from their site:

Airbrake collects errors generated by other applications, and aggregates the results for review.

First of all, we need an account! If you don't have one, go and grab one at Airbrake.io. They have many plans (including a free one).

Getting started

I'm assuming that you already have composer installed. If not got to getcomposer.org.

First we need to add the Airbrake composer package:

… 
"airbrake/airbrake-php": "dev-master",
… 

to our composer.json file and then composer update.

Notice something? Its not a specific L4 composer package. So we don't need to add the aliases and providers to our config/app.php.

Now that we have the package in our vendor directory we want to start sending all the exceptions to Airbrake.

Let's create a configuration file for our Airbrake API credentials. In the config directory add a new file called airbrake.php with the content:

<?php

return array(
    'active'        => true,
    'API'           => 'YOUR-API-KEY',
);

Simple and clean, right?

Now lets go to the project root and open the start/global.php file and change the App::error function:

… 

App::error(function(Exception $exception, $code)
{
    // keep saving the exception on the log file.
    Log::error($exception);

    // simple on/off mechanism 
    if(Config::get('airbrake.active'))
    {
        $config = new Airbrake\Configuration(Config::get('airbrake.API'), 
            array(
                'async' => true, 
                'environmentName' => App::environment(), 
                'url' => Request::url()
            ));
        $client = new Airbrake\Client($config);
        $client->notifyOnException($exception);
    }
});

…

Airbrake provide some configuration options. Check it at their GitHub repo.

If you don't want to send errors from you development environment just add a config/local/airbrake.php file with:

<?php

return array(
    'active'        => false,
);

make sure you have the environment set correctly.

And that's it… simple! :)

If you have any questions leave them on the comments or drop me a line on twitter @cossou!


Hélder

3 Responses
Add your response

This article has a perfect timing. Thank you!

over 1 year ago ·
over 1 year ago ·

Thanks a lot !

over 1 year ago ·