【问题标题】:Laravel Exception handler logs also the requestLaravel 异常处理程序也会记录请求
【发布时间】:2020-03-27 04:07:14
【问题描述】:

我正在开发一个 Laravel 5.8 项目,如果发生异常,我想在每日日志文件中记录导致异常的请求。

我已经在public function report(Exception $exception) 中尝试过这个

parent::render(request());

但它根本不起作用。我也试过这个

\Log::error(json_encode(request()));

但它记录了这个

local.ERROR: {"attributes":{},"request":{},"query":{},"server":{},"files":{},"cookies":{},"headers":{}}  

我该怎么做?我需要它来了解哪个请求导致了该异常,如果可能的话,我还需要记录其他值,但我认为解决了这个问题,我可以重用代码来记录其他值

【问题讨论】:

  • 请显示更多代码。
  • 没有更多代码,在Handler.php 内部,在公共函数public function report(Exception $exception) 内部我只有parent::report($exception);,如果您需要一些特定的其他部分,我还需要在那里记录请求代码,请告诉我,我会发布它们
  • 执行此代码的位置 - \Log::error(json_encode(request()));
  • @Dmitry 在 Exception/Handler.php` 文件中的 public function report(Exception $exception)
  • 尝试将 request 类型提示到方法的定义中。 public function report(Exception $exception, Request $request) { ...

标签: php laravel exception error-handling


【解决方案1】:

您不能只 json_encode() 整个请求,因为许多属性是私有/受保护的,并且需要使用 getter 进行访问。您将需要确定哪些价值观对您很重要,并做出适当的回应。

$response = [
    'method' => request()->method(),
    'url' => request()->url(),
    'full_url' => request()->fullUrl(),
    'data' => request()->all(),
];

然后您可以将响应数组作为第二个参数传递给日志处理程序,而无需使用json_encode()

\Log::error('Request details: ', $response);

【讨论】:

  • 为什么如果我将请求传递给 log::error 它会打印一个空请求?
猜你喜欢
  • 2015-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多