在 laravel 5.3 中被截断的错误日志

2022-08-31 00:08:32

我在我的laravel 5.3日志中有这个条目

2016-12-22 17:23:37] 本地时间.错误: GuzzleHttp\异常\客户端异常: 客户端错误: 导致响应: { “错误”: [ { “消息”: “消息生成被拒绝”, “描述”: “收件人地址由于客户 p 而被禁止(截断...)POST https://api.sparkpost.com/api/v1/transmissions400 Bad Request

为什么它会截断重要的错误消息?现在我无法弄清楚出了什么问题...


答案 1

由于您的请求会引发 Guzzle 异常,因此作为解决方法,您可以简单地尝试以下操作,而不是调用 ,$e->getMessage()

$e->getResponse()->getBody()->getContents();

如果不想修改 report() 方法。

对我来说很好。


答案 2

截断由 Guzzle 库完成。它仅显示响应的前 120 个字符。我假设这是因为回复可能很长。

如果要查看完整的消息,应该能够自定义如何处理大量异常。

将中的方法更新为如下所示: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);
}

注意:这是在方法中完成的,因此它只影响写入日志的内容。如果将异常转储到终端或浏览器,它仍将显示截断的消息。report


推荐