Email Yourself When Someone Encounter An Error In Your Laravel Application

profile picture

Email Yourself When Someone Encounter An Error In Your Laravel Application

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 ^^

about me

profile picture

I consider myself as an IT Business Artisan. Or Consultant CTO. I'm a self-taught Web Developper, coach and teacher. My main work is helping and guiding digital startups.

more about me

follow me

newsletter

A weekly email with the latests articles

support my work

Start trading on binance
  • BTC

    BTC

    18SY81ejLGFuJ9KMWQu5zPrDGuR5rDiauM

  • ETH

    ETH

    0x519e0eaa9bc83018bb306880548b79fc0794cd08

  • Monero

    XMR

    895bSneY4eoZjsr2hN2CAALkUrMExHEV5Pbg8TJb6ejnMLN7js1gLAXQySqbSbfzjWHQpQhQpvFtojbkdZQZmM9qCFz7BXU

recent posts

QBUS - MQTT Quick Tutorial

1
10 months ago

DDOS By Web Crawlers

1
1 year ago

Something Given

1
1 year ago

2024 © My Dynamic Production SRL All rights Reserved.