错误登录 laravel 5,错误发生在 url

Error log on laravel 5 with the url where error occured

我想在每个错误日志中附加错误发生前用户请求的页面的 url。日志已经告诉我错误发生的位置,但知道 url.

对我来说很有价值

我看到有一个 Handler.php 文件,但我怎样才能在其中附加 url?

这很简单,在你的 app/Exceptions/Handler.php 上面添加这两个导入:

use Request;
use Log;

然后在您的报告方法中添加:

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

现在,每当您遇到异常时,都会记录 当前 url 并且请求参数 GETPOST 也将被记录:

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

更好的方法:

App\Exceptions\Handler 中扩展 Laravel 的基础 context() 函数:

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 [];
    }
}

您可以在 App\Exceptions\Handler 中扩展 context() 方法。在此实现中,您保留原始方法并仅扩展数据数组。

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

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