Laravel 5.7如何使用URL登录404 [英] Laravel 5.7 How to Log 404 With URL

查看:149
本文介绍了Laravel 5.7如何使用URL登录404的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Laravel 5.7中记录404错误,但是我不知道如何打开它.除了记录404错误外,我还要记录所请求的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'],
],

根据错误处理文档:

异常处理程序的$ dontReport属性包含一个数组 不会记录的异常类型.例如,例外 由于404错误以及其他几种类型的错误而导致, 不会写入您的日志文件.您可以添加其他异常类型 根据需要添加到该数组:

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:

app/Exceptions/Handler中,$dontReport数组为空.

我通过拥有Blade文件resources/views/errors/404.blade.php

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

基于此答案app/Exceptions/Handler,中尝试了此代码,但日志中未显示任何内容:

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

在接受Mozammil的回答后更新,效果很好. 我已经将他的回答缩短到下面.不要忘记在处理程序文件中添加use Illuminate\Support\Facades\Log.

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

推荐答案

我也有类似的要求.这就是我实现它的方式.

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

我有一个辅助方法来确定它是否是404.

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

我还有另一种方法来实际记录404.

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

然后,要记录错误,我只需在render()方法中执行以下操作:

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

我不知道$internalDontReport.但是,在所有情况下,我的实现都对我有用:)

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

这篇关于Laravel 5.7如何使用URL登录404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆