2016-12-22 140 views
1

截斷我在我的laravel 5.3日誌錯誤日誌中laravel 5.3

2016-12-22 17:23:37] local.ERROR: GuzzleHttp\Exception\ClientException: Client error: POST https://api.sparkpost.com/api/v1/transmissions resulted in a 400 Bad Request response: { "errors": [ { "message": "Message generation rejected", "description": "recipient address suppressed due to customer p (truncated...)

爲什麼它截斷重要的錯誤信息這個項目嗎?現在我無法弄清楚發生什麼事了......

回答

4

截斷是由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方法完成的,因此它僅影響被寫入日誌。如果異常被轉儲到終端或瀏覽器,它仍將顯示被截斷的消息。

+0

我做了一些調整......問題是,$消息=(字符串)$Ë確實包含堆棧跟蹤。 substr($ msg,0,-135)將不起作用。要讀取結果,現在已經在錯誤信息的底部讀取了,在棧跟蹤的底部,實際上 – Toskan

+0

@Toskan你是對的,原來的實現是不正確的(正如我已經注意到的那樣,它沒有經過測試)。我修正了代碼,並且實際測試了它,所以它應該按原樣工作。我也改變了它,以便它將繼續包含堆棧跟蹤,而原始算法的設計使得只有消息沒有堆棧跟蹤就會被記錄下來。 – patricus

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

+0

這不建議。如果您修改供應商目錄中的代碼,則在下次執行「作曲者更新」時,您的更改將會丟失。 – patricus

+0

我完全意識到這一點,此外,它只適用於所述庫更新時,而不是每個作曲家更新。但是它可以幫助程序員找出最初的錯誤,解決問題並繼續前進。 – Toskan