Search code examples
phplaravelhttp-status-code-404

How to log 404 errors in Laravel 10?


Laravel doesn't log 404 errors by default. I want to change this behavior.

Most guides say to log it manually in the following block :

public function register(): void
{
    $this->reportable(function (Throwable $e) {
        // Handle 404 here
    });
}

but it seems like 404 exceptions don't even trigger this method for some reason.

Doing the following technically works but it's tedious to put everywhere I need it :

try {
     $model = User::findOrFail(99999);
} catch (ModelNotFoundException $e) {
     Log::error($e);
     throw $e;
}

I'm using Laravel 10.

What am I doing wrong ?


Solution

  • You can try with stopIgnoring method.

    https://laravel.com/docs/10.x/errors#ignoring-exceptions-by-type

    use Illuminate\Database\Eloquent\ModelNotFoundException;
     
    /**
     * Register the exception handling callbacks for the application.
     */
    public function register(): void
    {
        $this->stopIgnoring(ModelNotFoundException::class);
     
        // ...
    }