【问题标题】:Laravel - logging "route not found"Laravel - 记录“找不到路线”
【发布时间】:2020-02-18 02:42:27
【问题描述】:

找不到路由时,我看不到任何日志输出。我在开发模式下运行 laravel,在遇到不存在的路由时会看到此错误。

"message": "",
"exception": 
"Symfony\\Component\\HttpKernel\\Exception\\NotFoundHttpException",
"file":
"/var/www/html/Customer_Interface/vendor/laravel/framework/src/Illuminate/Routing/RouteCollection.php",
"line": 179,

但是,它不显示任何活动存储/日志。有没有办法捕捉到这个异常并在那里记录?

【问题讨论】:

  • 我不认为这是一个直接的答案,但我认为this SO thread 会很有帮助。

标签: laravel


【解决方案1】:

Laravel 默认不报告这些:

//src/Illuminate/Foundation/Exceptions/Handler.php

/**
     * A list of the internal exception types that should not be reported.
     *
     * @var array
     */
    protected $internalDontReport = [
        AuthenticationException::class,
        AuthorizationException::class,
        HttpException::class,
        HttpResponseException::class,
        ModelNotFoundException::class,
        SuspiciousOperationException::class,
        TokenMismatchException::class,
        ValidationException::class,
    ];

现在,您可以在 app/Exceptions/Handler.php 中导入该数组,然后删除 HttpExceptionModelNotFoundException,或者您想要的任何异常


    ...
    // app/Exceptions/Handler.php

    protected $internalDontReport = [
        AuthenticationException::class,
        AuthorizationException::class,
        // HttpException::class,
        HttpResponseException::class,
        // ModelNotFoundException::class,
        SuspiciousOperationException::class,
        TokenMismatchException::class,
        ValidationException::class,    
    ];
    ...

ModelNotFoundException 是当你有类似route::get('/articles/{article}','ArticleController@single'); 的路线时

并在控制器中分配为 function single(Article $article)$article 未找到。

选择要保留的内容和要注释掉的内容。

第二种方法是在同一 Handler.php 文件中的 report 方法中执行此操作

 /**
     * Report or log an exception.
     *
     * @param \Exception $exception
     *
     * @return void
     * @throws Exception
     */
    public function report(Exception $exception)
    {

        # Report 404 
        if ($exception instanceof NotFoundHttpException || $exception instanceof ModelNotFoundException) {
             # Log as warning
             Log::warning('Your message');
             # Or Log as info
             Log::info($exception);
             // ...
        }
        ....
     }

您可以在此Laravel Logging 中找到有关登录的更多信息 还有Laravel Errors

希望这能回答你的问题!

【讨论】:

    【解决方案2】:

    Handler.php 内(在Exceptions 目录中)有一个$dontReport 数组,其中包含不应报告的异常类。据我所知,上述 NotFoundHttpException 作为默认值列在那里。如果将其从数组中删除,则相关异常应在日志中表示。

    【讨论】:

      猜你喜欢
      • 2020-07-14
      • 2016-09-16
      • 2019-01-30
      • 2018-12-09
      • 1970-01-01
      • 2015-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多