Blog Detail

29

Sep
Send Error Notifications of Laravel  App to Email & Slack cover image

arrow_back Send Error Notifications of Laravel App to Email & Slack

The Laravel Error Notifier package sends you an email and a slack notification whenever 500 internal server errors happen in your application. This package only works for laravel 8 and 9.

How to install

Run this command in the terminal.

composer require jgodstime/laravel-error-notifier

In your /app/Exceptions/Handler.php file, in the register method, add \ErrorNotifier\Notify\Helper::getError($e); this must be inside reportable callback

    public function register()
    {
    	$this->reportable(function (Throwable  $e)  {
    		 \ErrorNotifier\Notify\Helper::getError($e);
    	});
    }

Disable Instant Notification

By default this package sends a notification immediately after the error occurred i.e without waiting for the users description of the error, you can disable this by adding
NOTIFIER_INSTANT=false in your .env file.

Publish the vendor files

php artisan vendor:publish --provider="ErrorNotifier\Notify\ErrorNotifierServiceProvider"

Now the vendor files are published in their respective paths

Setup your email driver

To set up your email driver, you can use mailtrap for test purposes.

    MAIL_DRIVER=smtp
    MAIL_HOST=smtp.mailtrap.io
    MAIL_PORT=587
    MAIL_USERNAME=528a733...
    MAIL_PASSWORD=73c29...
    MAIL_ENCRYPTION=tls
    MAIL_FROM_ADDRESS=mygoogle@gmail.com
    MAIL_FROM_NAME="${APP_NAME}"

Add your slack webhook (Optional)

To set up your slack webhook URL

LOG_SLACK_WEBHOOK_URL=https://hooks.slack.com/services/T....

To test this, simply add $array[‘key1’] = ‘john’; $data = $array[‘key2’]; in one of your routes, then hit the route In your browser.

    Route::get('/convert/file', function(){
    	 $array['key1'] = 'john';
        $data  = $array['key2'];
    });

Notice that we are trying to access an array with key2 that doesn’t exist, this will throw an error and the package will send the error as a notification to your email as well as the trace.

Also, you must turn APP_DEBUG=false in your .env file.

Send Email to Multiple Recipients

You can send notifications to multiple recipients by adding multiple emails as a string in a comma-separated format without space

NOTIFIER_EMAIL="hello1@example.com,hello2@example.com"

Change Redirect Page URL
You can change the page user is taken to after submitting the form, by default it goes to the home page (/)

NOTIFIER_REDIRECT_URL='/thank-you'

Closing Note

Codebrisk is here to help you grow your business. Our expert developers have plenty of experience in Laravel Custom Web Application Development. We can enhance the performance of applications with easy syntax, third-party API integrations, and built-in functions, followed by a high level of security to prevent unauthorized access to data. We always provide a customized website solution tailored to our client’s needs. So if you’re interested, you can get in touch with us or get a free quote from us.

Published at : 29-09-2022

Author : Rizwan Aslam
AUTHOR
Rizwan Aslam

I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.

Launch your project

Launch project