Email Yourself When Someone Encounter An Error In Your Laravel Application

Email Yourself When Someone Encounter An Error In Your Laravel Application


Oct 23 2015, 19:05 in Web Development

Did you ever feel anxious about deploying a large application in production for the first time ? You are afraid that there are still some bugs somewhere, because you don't have a full testing team to report everything to you ? And you wonder : "If a user encounter a bug when using my website, how would I know ?".

Well I've been thinking about that a lot, because we are soon releasing a new version of the website wearethemodels.co.

So a cool thing to do would be to get an email each time an error is triggered in our application, with a lot of details, like the Laravel debug page. You'll be glad to know that it can totally be done, and pretty easily !

You have two ways to do that, but we will only cover the best one.

1 In your app/Exceptions/Handler.php

You can add some new logic to the report() method, and use Monolog package to trigger an email event. This is pretty customizable, and the documentation is really good.

2 Or, you can use a free reporting tool such as Rollbar

Rollbar.com is a free tool that provides a pluggable API through which you can send all your errors. The website will then diplay every information about the errors, with some stats and other cool features (such as "resolve").

Rollbar screenshot

The first thing to do is to register on their website and to chose the free plan, obviously.

Once done, simply install this Laravel package: https://github.com/jenssegers/Laravel-Rollbar by following the really simple Installation Instructions.

**In your code : **

The package suggests you to extend the report() method like this : 

public function report(Exception $e)
{
    Log::error($e);

    return parent::report($e);
}

But I strongly prefer this configuration :

public function report(Exception $e)
{
    if (env('APP_ENV') != 'local') {
        if (Auth::check()) {
            \Log::error($e, ['person' => ['id' => Auth::id()]]);
        } else {
            \Log::error($e);
        }
    }
    return parent::report($e);
}

By doing as above, you will avoid triggering the Rollbar API while working on your local machine. We only want debug infos on our dev and productions servers.
Also, if the user is logged in, you will get his ID, and you'll be able to inspect his account if needed :)

That's all ! Enjoy Debugging ^^


Share:
Like:

Disable AdBlock on this domain and offer me a cup of coffee :)