How to Log 404 Page Not Found Errors in Laravel

Logging Page Not Found errors is pretty simple in Laravel. Every Laravel installation has an exceptions handler used to display error messages. You can just edit the render method of this class and add a log message for 404 status codes.

app/Exceptions/Handler.php:

public function render($request, Throwable $e)
{
    $e = $this->prepareException($e);
    $status_code = (int) method_exists($e, 'getStatusCode') ? $e->getStatusCode() : $e->getCode();
    if ($status_code === 404) {
        Log::error('Page not found: ' . $request->url());
    }

    return parent::render($request, $e);
}

There are no comments yet.