Error log on Laravel 5 with the URL where error occurred
Asked Answered
P

3

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?

Porbeagle answered 10/2, 2016 at 17:32 Comment(0)
B
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"}} 
Bot answered 10/2, 2016 at 19:25 Comment(5)
Works like a charm. Thanks guys, very usefull.Porbeagle
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!Monroemonroy
I think its better not to include the inputs anyway, with url + line of error it's enough to start debuggingHuss
It should be: use Illuminate\Http\Request; use Illuminate\Support\Facades\Log;Unwonted
@MartijnHiemstra these are aliased normally in config/app.phpRasputin
R
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 [];
    }
}
Rema answered 9/3, 2018 at 15:1 Comment(3)
Not working in Laravel 5.2. (I know its old version but stuck on this due to some business reasons)Schottische
Which page do I add this code and how should I extend Laravel's context and connect it with my app/Exceptions/HandlerSynecious
@EmmanuelDavid you add it to the "App\Exceptions\Handler.php", it all says in the descriptionRema
L
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());
}
Louisville answered 3/7, 2018 at 20:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.