user1978109
user1978109

Reputation: 737

Laravel - logging "route not found"

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?

Upvotes: 3

Views: 897

Answers (2)

Emad Ha
Emad Ha

Reputation: 1222

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,
    ];

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 is when you have a route like route::get('/articles/{article}','ArticleController@single');

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.

Second approach is to do this in the report method in the same Handler.php file

 /**
     * 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);
             // ...
        }
        ....
     }

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

Hope this answers your question!

Upvotes: 4

mynd
mynd

Reputation: 794

Within Handler.php (in directory Exceptions) there is an $dontReport array that holds the exception classes that shall not be reported. As far as I know the said NotFoundHttpException is listed there as default. If you remove it from the array the related exceptions should be represented in the logs.

Upvotes: 1

Related Questions