Tos*_*kan 4 php logging laravel
我在laravel 5.3日志中有这个条目
2016-12-22 17:23:37] local.ERROR:GuzzleHttp\Exception\ClientException:客户端错误:
POST https://api.sparkpost.com/api/v1/transmissions导致400 Bad Request响应:{"errors":[{"message":"消息生成被拒绝","描述": "收件人地址因客户p而被抑制(截断...)
为什么它会截断一条重要的错误信息?现在我无法弄清楚出了什么问题......
Ard*_*rda 14
因为您的请求会抛出 Guzzle 异常,作为一种解决方法$e->getMessage(),您可以简单地尝试:
$e->getResponse()->getBody()->getContents();
Run Code Online (Sandbox Code Playgroud)
如果您不想修改 report() 方法。
对我很好。
截断是由Guzzle库完成的.它仅显示响应的前120个字符.我假设这是因为回复可能会很长.
如果您想查看完整的消息,您应该能够自定义如何处理guzzle异常.
将report()您的方法更新为app/Exceptions/Handler.php:
public function report(Exception $exception)
{
// this is from the parent method
if ($this->shouldntReport($exception)) {
return;
}
// this is from the parent method
try {
$logger = $this->container->make(\Psr\Log\LoggerInterface::class);
} catch (Exception $ex) {
throw $exception; // throw the original exception
}
// this is the new custom handling of guzzle exceptions
if ($exception instanceof \GuzzleHttp\Exception\RequestException) {
// get the full text of the exception (including stack trace),
// and replace the original message (possibly truncated),
// with the full text of the entire response body.
$message = str_replace(
rtrim($exception->getMessage()),
(string) $exception->getResponse()->getBody(),
(string) $exception
);
// log your new custom guzzle error message
return $logger->error($message);
}
// make sure to still log non-guzzle exceptions
$logger->error($exception);
}
Run Code Online (Sandbox Code Playgroud)
注意:这是在report方法中完成的,因此它只会影响写入日志的内容.如果将异常转储到终端或浏览器,它仍将显示截断的消息.
作为替代解决方案
修补程序 RequestException.php
ta_integration/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php
更换
$size = $body->getSize();
$summary = $body->read(120);
$body->rewind();
if ($size > 120) {
Run Code Online (Sandbox Code Playgroud)
例如:
$size = $body->getSize();
$summary = $body->read(999);
$body->rewind();
if ($size > 999) {
Run Code Online (Sandbox Code Playgroud)
功能
getResponseBodySummary