Cookie Consent by Free Privacy Policy Generator 📌 How can I be notified by e-mail when a critical error occurs in a Laravel application?


✅ How can I be notified by e-mail when a critical error occurs in a Laravel application?


💡 Newskategorie: Programmierung
🔗 Quelle: dev.to

Although Laravel manages logs efficiently, it can sometimes be crucial to keep a close eye on errors during the development of certain projects. This tip will enable you to receive an e-mail as soon as a critical error occurs on your server. It is important to note that it is recommended to send only critical errors to avoid saturating your inbox.
The simplest approach (in my opinion) is to use bug-tracking software such as Sentry (or equivalent); this will help regulate notifications. But what if I don't want these services? I can do my own thing.

Creation of the ExceptionMail mail class

Run this command in your Laravel project:

php artisan make:mail ExceptionMail

Once the mail class is created, we'll modify it. Instead of creating a separate view, I'll inject HTML content directly.

namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;

class ExceptionMail extends Mailable
{
    use Queueable, SerializesModels;
    public function __construct(private string $htmlString)
    {
        //
    }
    public function envelope(): Envelope
    {
        return new Envelope(
            subject: 'A critical error occurred'
        );
    }

    public function content(): Content
    {
        return new Content(
            htmlString: $this->htmlString
        );
    }
}

Sending the Email

In the app/Exceptions/Handler.php class, you'll find Laravel's base exception handling class. Let's add a private sendMail method to this class and called it in the rergister method.

namespace App\Exceptions;
use App\Mail\ExceptionMail;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Support\Facades\Mail;
use Throwable;

class Handler extends ExceptionHandler
{
    public function register(): void
    {
        $this->reportable(function (Throwable $e) {
            $this->sendMail($e);
        });
    }

    private function sendMail(Throwable $e)
    {
        if (App::isLocal()) {
            return;
        }

        //Developer emails
        $emails = ["[email protected]", "[email protected]"];

        $htmlStr = "An error occurred while processing the request. Here is the error";
        //You should normally return the file, the line, the code and the message, so that you have an idea of what it's about without consulting the log
        $htmlStr .= $e->getMessage();
        try {
            Mail::to($emails)->send(new ExceptionMail($htmlStr));
        } catch (\Throwable $th) {
            //Your code
        }
    }
}

Note:

  • I used Laravel 9; please adapt according to your version.
  • Ensure not to send all exceptions, but only critical errors.
  • It's not advisable to send email notifications in production mode.

If you want to simplify form validation in your Laravel projects and save time, I recommend trying out this library Trivule.

Follow me on Twitter and Dev Community for more tips and shares.

...

✅ after deployment in live server, 404 error occurs on refresh of urls


📈 31.64 Punkte

✅ Error 80270254 occurs when you play content or game on your Xbox One console


📈 31.64 Punkte

✅ Customize Error Views in Laravel #laravel #php #laravelhint #thecodeholic


📈 29.81 Punkte

✅ Hosting a simple Laravel application using Turso on Laravel Forge


📈 28.96 Punkte

✅ How companies should recover when password breach occurs


📈 25.13 Punkte

✅ What is Data Breach? Why and How It occurs? How To Prevent Data Breach


📈 25.13 Punkte

✅ Cybersecurity recovery is a process that starts long before a cyberattack occurs


📈 25.13 Punkte

✅ Twitch Will Ban Users For 'Severe Misconduct' That Occurs Away From Its Site


📈 25.13 Punkte

✅ Maximum: [mijn.werkenbijdefensie.nl] Denial of service occurs due to lack of email length confirmation


📈 25.13 Punkte

✅ Data Breach Occurs at Agency in Charge of Secure White House Communications


📈 25.13 Punkte

✅ Using watchdog package to trigger reboot when fencing occurs.


📈 25.13 Punkte

✅ 68% of Data Breach Occurs Due to Social Engineering Attacks


📈 25.13 Punkte

✅ 🚀 Boost Your Laravel Performance with Real-Time Laravel N+1 Query Detection! 🛠️


📈 23.3 Punkte

✅ Deploy Laravel with Github Actions - Part 45 | Laravel Social Media Website


📈 23.3 Punkte

✅ Laravel Reverb: Fitur Websocket Server pada Laravel 11


📈 23.3 Punkte

✅ I built $7,000 Laravel Project for YouTube #laravel #laravelproject #laravelframework


📈 23.3 Punkte

✅ Dynamic Report Generation in Laravel: Introducing `laravel-dynamic-report-generator`


📈 23.3 Punkte

✅ I built $7000 Laravel Project #laravel #laravelproject #laraveldeveloper #laravelframework


📈 23.3 Punkte

✅ CVE-2024-29291 | Laravel Framework 8/9/10/11 storage/logs/laravel.log information disclosure


📈 23.3 Punkte

✅ Project setup with Laravel Sail - Part 1 | Laravel Social Media Website


📈 23.3 Punkte

✅ Laravel-10 Laravel-11 comparison, changes


📈 23.3 Punkte

✅ Laravel Boiler Template: Elevate Your Laravel Experience


📈 23.3 Punkte

✅ Laravel: Pengenalan Fitur Reverb Di Laravel


📈 23.3 Punkte

✅ I Built Laravel 10 Blog with Admin Panel #laravel


📈 23.3 Punkte

✅ Build and Deploy Real Time Messaging App with Laravel and React (with Laravel Reverb)


📈 23.3 Punkte

✅ Low CVE-2019-17494: Laravel-bjyblog project Laravel-bjyblog


📈 23.3 Punkte

✅ A guide to feature flags in Laravel using Laravel Pennant


📈 23.3 Punkte

✅ Low CVE-2019-17433: Laravel-admin Laravel-admin


📈 23.3 Punkte











matomo

Datei nicht gefunden!