phplaravellaravel-5error-loggingerror-log

Error log on Laravel 5 with the URL where error occurred


I want to append to each error log, the URL of the page the user requested before the error occurred. The logs already give me where the error occurs, but it's valuable for me know the URL.

I saw there is a Handler.php file but how can I append the URL there?


Solution

  • It's quite simple, on your app/Exceptions/Handler.php on top the of it add this two imports:

    use Request;
    use Log;
    

    Then on your report method add this:

    public function report(Exception $e) {
        Log::info($e->getMessage(), [
            'url' => Request::url(),
            'input' => Request::all()
        ]);
        return parent::report($e);
    }
    

    Now whenever you get an exception the current url is logged and the request parameters either GET or POST will also be logged:

    [2016-02-10 19:25:13] local.INFO: Error Processing Request {"url":"http://localhost:8002/list","input":{"name":"fabio","surname":"antunes"}}