Laravel-记录“未找到路由" [英] Laravel - logging "route not found"

查看:174
本文介绍了Laravel-记录“未找到路由"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I do not see any log output when a route is not found. I am running laravel in development mode and I can see this error when hitting a route that does not exist.

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

但是,它不显示任何活动存储/日志.无论如何,有没有捕捉到该异常并将其记录在那里?

However, it does not show any activity storage/logs. Is there anyway to catch this exception and log it there?

推荐答案

Laravel默认不报告以下内容:

Laravel by default doesn't report these:

//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 中导入该数组,然后删除 HttpException ModelNotFoundException ,或者您想要这样的任何异常

Now, you can in app/Exceptions/Handler.php you can have that array imported then remove the HttpException and ModelNotFoundException, or whichever Exception you want like so


    ...
    // 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 .

and have that assigned in the controller as function single(Article $article) and $article is not found.

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

Pick what to keep and what to comment out.

 /**
     * 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日志记录中找到有关登录的更多信息.还有 Laravel错误

You can find more about loggin in this Laravel Logging And Laravel Errors

希望这能回答您的问题!

Hope this answers your question!

这篇关于Laravel-记录“未找到路由"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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