在laravel 5.3中截断的错误日志

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() 方法。

对我很好。

  • 该解决方案应该是公认的答案。又好又简单。 (2认同)

pat*_*cus 8

截断是由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方法中完成的,因此它只会影响写入日志的内容.如果将异常转储到终端或浏览器,它仍将显示截断的消息.


Tos*_*kan 8

作为替代解决方案

修补程序 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

  • 我完全知道这一点,此外它仅适用于所述库更新时,而不适用于每个作曲家更新.但它有助于程序员弄清楚原始错误是什么,解决问题,然后继续前进. (5认同)
  • 不建议这样做。如果您修改了供应商目录中的代码,那么下次您进行“作曲者更新”时,所做的更改将丢失。 (2认同)