Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.7 How to Log 404 With URL

I want to log 404 errors in Laravel 5.7, but I don't understand how to turn this on. Additional to logging 404 errors, I'd like to log the URL that was requested. Other errors are logged correctly.

.env

APP_DEBUG=true
LOG_CHANNEL=stack

config/logging.php

'stack' => [
    'driver' => 'stack',
    'channels' => ['daily'],
],

Per the Error Handling documentation:

The $dontReport property of the exception handler contains an array of exception types that will not be logged. For example, exceptions resulting from 404 errors, as well as several other types of errors, are not written to your log files. You may add other exception types to this array as needed:

In app/Exceptions/Handler the $dontReport array is empty.

I have customized the 404 view by having a Blade file resources/views/errors/404.blade.php

Based on this answer I've tried this code in app/Exceptions/Handler, but nothing shows up in the logs:

public function report(Exception $exception)
{
    if ($this->isHttpException($exception)) {
        if ($exception instanceof NotFoundHttpException) {
            Log::warning($message);
            return response()->view('error.404', [], 404);
        }
        return $this->renderHttpException($exception);
    }

    parent::report($exception);
}

UPDATE after accepting Mozammil's answer which works fine. I've shortened his answer to the below. Don't forget to add use Illuminate\Support\Facades\Log to the Handler file.

public function render($request, Exception $exception)
{
    if ($exception instanceof \Symfony\Component\HttpKernel\Exception\NotFoundHttpException) {
        Log::warning('404: ' . $request->url());
        return response()->view('errors.404', [], 404);
    }
    return parent::render($request, $exception);
}
like image 805
eskimo Avatar asked Dec 23 '22 01:12

eskimo


1 Answers

I have a similar requirement. Here's how I achieved it.

I have a helper method to determine if it's a 404.

private function is404($exception)
{
    return $exception instanceof \Illuminate\Database\Eloquent\ModelNotFoundException
            || $exception instanceof \Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
}

I also have another method to actually log the 404.

private function log404($request) 
{
    $error = [
        'url'    => $request->url(),
        'method' => $request->method(),
        'data'   => $request->all(),
    ];

    $message = '404: ' . $error['url'] . "\n" . json_encode($error, JSON_PRETTY_PRINT);

    Log::debug($message);
}

Then, to log the error, I just do something like this in the render() method:

public function render($request, Exception $exception)
{
    if($this->is404($exception)) {
        $this->log404($request);
    }

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

I didn't know about the $internalDontReport. However, in all cases, my implementation worked for me :)

like image 172
Mozammil Avatar answered Feb 12 '23 00:02

Mozammil