Laravel 5.7 How to Log 404 With URL

核能气质少年 提交于 2020-05-13 07:49:20

问题


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);
}

回答1:


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 :)




回答2:


I use Telescope

Laravel Telescope
Laravel Telescope is an elegant debug assistant for the Laravel framework. Telescope provides insight into the requests coming into your application, exceptions, log entries, database queries, queued jobs, mail, notifications, cache operations, scheduled tasks, variable dumps and more.

https://laravel.com/docs/5.7/telescope



来源:https://stackoverflow.com/questions/54392184/laravel-5-7-how-to-log-404-with-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!