簡體   English   中英

錯誤日志在 laravel 5.3 中被截斷

[英]error log truncated in laravel 5.3

我的 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": "Message generation rejected", "description": "recipient address suppressed due to customer p (truncated...)

為什么它會截斷重要的錯誤消息? 現在我不知道出了什么問題......

因為你的要求引發的,而不是調用一個狂飲例外,作為一種解決方法, $e->getMessage()你可以簡單地嘗試:

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

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

對我很好。

截斷由 Guzzle 庫完成。 它只顯示響應的前 120 個字符。 我假設這是因為響應可能很長。

如果您想查看完整消息,您應該能夠自定義處理 guzzle 異常的方式。

app/Exceptions/Handler.phpreport()方法更新為如下所示:

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方法中完成的,所以它只影響寫入日志的內容。 如果異常被轉儲到終端或瀏覽器,它仍然會顯示截斷的消息。

作為替代解決方案:

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

編輯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)

這些解決方案都沒有幫助我。 我在這里找到了一個有幫助的解決方案。 由用戶 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;

}

在供應商/guzzlehttp/psr7/src/functions.php

有這個 function:

 function get_message_body_summary(MessageInterface $message, $truncateAt = 120) { return Message::bodySummary($message, $truncateAt); }

只需將 $truncateAt = 120 更改為您喜歡的任何值

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM