【问题标题】:error log truncated in laravel 5.3在 laravel 5.3 中截断的错误日志
【发布时间】:2017-05-08 15:56:03
【问题描述】:

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

2016-12-22 17:23:37] 本地。错误: GuzzleHttp\Exception\ClientException:客户端错误:POST https://api.sparkpost.com/api/v1/transmissions 导致400 Bad Request 响应:{“错误”:[{“消息”:“消息生成 拒绝”,“描述”:“由于收件人地址被抑制 客户 p(截断...)

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

【问题讨论】:

    标签: php laravel logging


    【解决方案1】:

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

    如果您想查看完整消息,您应该能够自定义处理 guzzle 异常的方式。

    app/Exceptions/Handler.php 中的report() 方法更新为:

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

    【讨论】:

    • 我做了一些调整...问题是 $message = (string) $e 确实包含堆栈跟踪。 substr($msg, 0, -135) 将无法正常工作。要阅读结果,现在必须阅读错误消息的 VERY BOTTOM,实际上是在堆栈跟踪的底部
    • @Toskan 你是对的,原始实现不正确(正如我所指出的,它未经测试)。我已经更正了代码,并对其进行了实际测试,因此它应该可以正常工作。我还对其进行了更改,使其继续包含堆栈跟踪,而原始算法的设计是只记录消息而不记录堆栈跟踪。
    • 在 laravel 4.1 中如何处理
    • 这对我不起作用。第二个 if 语句不正确,因此它不会运行。
    • @MartijnHiemstra 这是故意的。您只希望在处理 \GuzzleHttp\Exception\RequestException 时运行额外的代码。
    【解决方案2】:

    作为替代解决方案:

    修补程序RequestException.php

    ta_integration/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php

    替换

    $size = $body->getSize();
    $summary = $body->read(120);
    $body->rewind();
    
    if ($size > 120) {
    

    例如:

        $size = $body->getSize();
        $summary = $body->read(999);
        $body->rewind();
    
        if ($size > 999) {
    

    功能

    getResponseBodySummary

    【讨论】:

    • 不建议这样做。如果您修改供应商目录中的代码,您的更改将在您下次执行composer update 时丢失。
    • 我完全清楚这一点,而且它仅适用于所述库更新时,而不适用于每个作曲家更新。但它可以帮助程序员找出最初的错误是什么,解决问题,然后继续前进。
    【解决方案3】:

    由于您的请求会引发 Guzzle Exception,因此作为一种解决方法,您可以简单地尝试:

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

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

    对我来说很好。

    【讨论】:

    • 这个解决方案应该是公认的答案。漂亮而简单。
    • 我接受了这个答案,但没有验证它是否真的有效......希望它可以。干杯
    • @Toskan 你应该在哪里做这个?您的问题是关于日志文件中的错误。如果你想解决这个问题,你要么需要修改报告方法,如我的答案所示(它使用上面的代码来获取消息的内容),要么像你的答案那样修复 guzzle。
    【解决方案4】:

    这里的这些解决方案都没有帮助我。我找到了一个有帮助的解决方案here。由用户 sidk2020 提供。如果链接断开,这是他的解决方案:

    我做了一些非常冒险的事情。我修改了 guzzel 的异常处理程序

    guzzel 故意只读取 120 字节的信息并在其旁边截断打印。

    文件位于:/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php

    所以我修改了那个函数,下面是我的函数的样子:

    public static function getResponseBodySummary(ResponseInterface $response) { 
        $body = $response->getBody();
    
        if (!$body->isSeekable() || !$body->isReadable()) {
            return null;
        }
    
        $size = $body->getSize();
    
        if ($size === 0) {
            return null;
        }
    
        // Matches any printable character, including unicode characters:
        // letters, marks, numbers, punctuation, spacing, and separators.
        if (preg_match('/[^\pL\pM\pN\pP\pS\pZ\n\r\t]/', $body)) {
            return null;
        }
    
        return $body;
    
    }
    

    【讨论】:

      【解决方案5】:

      编辑vendor/guzzlehttp/psr7/src/Message.php

      public static function bodySummary(MessageInterface $message, $truncateAt = 999)
      

      编辑vendor/guzzlehttp/psr7/src/functions.php

      function get_message_body_summary(MessageInterface $message, $truncateAt = 999)
      

      【讨论】:

      • 太棒了,这是一个很好的修复:D
      猜你喜欢
      • 1970-01-01
      • 2012-09-12
      • 1970-01-01
      • 2016-07-29
      • 1970-01-01
      • 2021-01-01
      • 1970-01-01
      • 2010-10-09
      • 2023-03-12
      相关资源
      最近更新 更多