简体   繁体   中英

How to send additional data to the laravel.log file?

I'm getting some error messages in the laravel.log file, but the error description doesn't help me to find the problem. I want to register some extra data, to be specific, the request URL and the request data , in all the errors reported to the log file. How can I add those two things?

[2015-11-09 13:30:01] production.ERROR: exception 'Illuminate\Session\TokenMismatchException' in /bootstrap/cache/compiled.php:2888
Stack trace:
#0 [internal function]: Illuminate\Foundation\Http\Middleware\VerifyCsrfToken->handle(Object(Illuminate\Http\Request), Object(Closure))
#1 /bootstrap/cache/compiled.php(9464): call_user_func_array(Array, Array)
#2 /app/Http/Middleware/CheckForMaintenanceMode.php(39): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#3 [internal function]: App\Http\Middleware\CheckForMaintenanceMode->handle(Object(Illuminate\Http\Request), Object(Closure))
#4 /bootstrap/cache/compiled.php(9464): call_user_func_array(Array, Array)
#5 /bootstrap/cache/compiled.php(12789): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#6 [internal function]: Illuminate\View\Middleware\ShareErrorsFromSession->handle(Object(Illuminate\Http\Request), Object(Closure))
#7 /bootstrap/cache/compiled.php(9464): call_user_func_array(Array, Array)
#8 /bootstrap/cache/compiled.php(11412): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#9 [internal function]: Illuminate\Session\Middleware\StartSession->handle(Object(Illuminate\Http\Request), Object(Closure))
#10 /bootstrap/cache/compiled.php(9464): call_user_func_array(Array, Array)
#11 /bootstrap/cache/compiled.php(12530): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#12 [internal function]: Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse->handle(Object(Illuminate\Http\Request), Object(Closure))
#13 /bootstrap/cache/compiled.php(9464): call_user_func_array(Array, Array)
#14 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#15 /bootstrap/cache/compiled.php(9454): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#16 /bootstrap/cache/compiled.php(2209): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#17 /bootstrap/cache/compiled.php(2192): Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter(Object(Illuminate\Http\Request))
#18 /public/index.php(54): Illuminate\Foundation\Http\Kernel->handle(Object(Illuminate\Http\Request))
#19 {main}  

In /app/Exceptions/Handler.php modify the public function report(Exception $e) .

// ...
use Request;

// ...
public function report(Exception $e)
{
    if ($this->shouldReport($e)) {
        $request = Request::all();

        $this->log->error("request url: " . Request::url());
        $this->log->error("request data: " . json_encode($request));
    }

    parent::report($e);
}

Finally, the reported error in laravel.log will contain the request URL and data.

[2015-11-12 03:41:14] local.ERROR: request url: http://example.com/path  
[2015-11-12 03:41:14] local.ERROR: request data: {"key":"value"}
[2015-11-12 03:41:14] local.ERROR: exception 'Illuminate\Session\TokenMismatchException' in /bootstrap/cache/compiled.php:2888
Stack trace:
#0 [internal function]: Illuminate\Foundation\Http\Middleware\VerifyCsrfToken->handle(Object(Illuminate\Http\Request), Object(Closure))
#1 /bootstrap/cache/compiled.php(9464): call_user_func_array(Array, Array)
#2 /app/Http/Middleware/CheckForMaintenanceMode.php(39): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#3 [internal function]: App\Http\Middleware\CheckForMaintenanceMode->handle(Object(Illuminate\Http\Request), Object(Closure))
#4 /bootstrap/cache/compiled.php(9464): call_user_func_array(Array, Array)
#5 /bootstrap/cache/compiled.php(12789): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#6 [internal function]: Illuminate\View\Middleware\ShareErrorsFromSession->handle(Object(Illuminate\Http\Request), Object(Closure))
#7 /bootstrap/cache/compiled.php(9464): call_user_func_array(Array, Array)
#8 /bootstrap/cache/compiled.php(11412): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#9 [internal function]: Illuminate\Session\Middleware\StartSession->handle(Object(Illuminate\Http\Request), Object(Closure))
#10 /bootstrap/cache/compiled.php(9464): call_user_func_array(Array, Array)
#11 /bootstrap/cache/compiled.php(12530): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#12 [internal function]: Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse->handle(Object(Illuminate\Http\Request), Object(Closure))
#13 /bootstrap/cache/compiled.php(9464): call_user_func_array(Array, Array)
#14 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#15 /bootstrap/cache/compiled.php(9454): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#16 /bootstrap/cache/compiled.php(2209): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#17 /bootstrap/cache/compiled.php(2192): Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter(Object(Illuminate\Http\Request))
#18 /public/index.php(54): Illuminate\Foundation\Http\Kernel->handle(Object(Illuminate\Http\Request))
#19 {main}  

It is clear that you have TokenMismatchException

How can i fix it

You can fix it by sending the _token that is generated by Laravel.

You can get the _token by default in your form if you use Form Helper, if not you can do it manually by passing it as hidden

<input type="hidden" name="_token" value="{{ csrf_token() }}">

Note :

By default Laravel will record all the errors in .log file inside storage/logs/laravel-date.log.

If you want to record those logs in then you need to do try catch and record manually inside db.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM