我想在 Laravel 5.7 中记录 404 错误,但我不明白如何打开它。除了记录 404 错误之外,我还想记录所请求的 URL。其他错误已正确记录。
.env
APP_DEBUG=true
LOG_CHANNEL=stack
配置/logging.php
'stack' => [
'driver' => 'stack',
'channels' => ['daily'],
],
Per the 错误处理文档 https://laravel.com/docs/5.7/errors:
异常处理程序的 $dontReport 属性包含一个数组
不会记录的异常类型。例如,异常
由 404 错误以及其他几种类型的错误导致,
不会写入您的日志文件。您可以添加其他异常类型
根据需要添加到该数组:
In app/Exceptions/Handler
the $dontReport
数组为空。
我通过 Blade 文件自定义了 404 视图resources/views/errors/404.blade.php
基于这个答案 https://stackoverflow.com/questions/52261128/laravel-5-7-error-404-handling-page-location/52262132我已经尝试过这段代码app/Exceptions/Handler,
但日志中没有显示任何内容:
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
到处理程序文件。
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);
}