13

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?

3 Answers 3

25

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"}} 
5
  • Works like a charm. Thanks guys, very usefull. Feb 11, 2016 at 2:56
  • 10
    I found out the hard way: be sure to unset password and password_confirmation from the Request::all() array before writing that to logs. Major security risk!
    – Ryan
    May 17, 2017 at 22:07
  • I think its better not to include the inputs anyway, with url + line of error it's enough to start debugging
    – Amir Bar
    Aug 13, 2017 at 11:59
  • It should be: use Illuminate\Http\Request; use Illuminate\Support\Facades\Log; May 25, 2020 at 16:05
  • @MartijnHiemstra these are aliased normally in config/app.php Sep 4, 2020 at 17:17
21

Even better way of doing this:

In App\Exceptions\Handler extend Laravel's base context() function:

use Throwable;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Request;


/**
 * Get the default context variables for logging.
 *
 * @return array
 */
protected function context()
{
    try {
        return array_filter([
            'url' => Request::fullUrl(),
            'input' => Request::except(['password', 'password_confirmation']),
            'userId' => Auth::id(),
            'email' => Auth::user() ? Auth::user()->email : null,
        ]);
    } catch (Throwable $e) {
        return [];
    }
}
4
  • 3
    This is a better answer.
    – Tainmar
    Jan 10, 2019 at 10:25
  • Not working in Laravel 5.2. (I know its old version but stuck on this due to some business reasons) Feb 29, 2020 at 12:52
  • Which page do I add this code and how should I extend Laravel's context and connect it with my app/Exceptions/Handler Nov 27, 2020 at 14:20
  • @EmmanuelDavid you add it to the "App\Exceptions\Handler.php", it all says in the description
    – fsasvari
    Dec 3, 2020 at 14:26
4

You can extend context() method in App\Exceptions\Handler. In this implementation, you keep original method and just expand the data array.

protected function context()
{
    try {
        $context = array_filter([
            'url' => Request::url()
        ]);
    } catch (Throwable $e) {
        $context = [];
    }

    return array_merge($context, parent::context());
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.