Merge pull request #10101 from nupplaphil/fix/log_toString

Fix object handling inside log arrays
2022.09-rc
Hypolite Petovan 2021-03-28 20:06:58 -04:00 zatwierdzone przez GitHub
commit dffd07fb74
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4AEE18F83AFDEB23
4 zmienionych plików z 45 dodań i 4 usunięć

Wyświetl plik

@ -103,6 +103,28 @@ abstract class AbstractLogger implements LoggerInterface
return strtr($message, $replace);
}
/**
* JSON Encodes an complete array including objects with "__toString()" methods
*
* @param array $input an Input Array to encode
*
* @return false|string The json encoded output of the array
*/
protected function jsonEncodeArray(array $input)
{
$output = [];
foreach ($input as $key => $value) {
if (method_exists($value, '__toString')) {
$output[$key] = $value->__toString();
} else {
$output[$key] = $value;
}
}
return @json_encode($output);
}
/**
* {@inheritdoc}
*/

Wyświetl plik

@ -161,8 +161,8 @@ class StreamLogger extends AbstractLogger
$logMessage .= $this->channel . ' ';
$logMessage .= '[' . strtoupper($level) . ']: ';
$logMessage .= $this->psrInterpolate($message, $context) . ' ';
$logMessage .= @json_encode($context) . ' - ';
$logMessage .= @json_encode($record);
$logMessage .= $this->jsonEncodeArray($context) . ' - ';
$logMessage .= $this->jsonEncodeArray($record);
$logMessage .= PHP_EOL;
return $logMessage;

Wyświetl plik

@ -195,8 +195,8 @@ class SyslogLogger extends AbstractLogger
$logMessage .= $this->channel . ' ';
$logMessage .= '[' . $this->logToString[$level] . ']: ';
$logMessage .= $this->psrInterpolate($message, $context) . ' ';
$logMessage .= @json_encode($context) . ' - ';
$logMessage .= @json_encode($record);
$logMessage .= $this->jsonEncodeArray($context) . ' - ';
$logMessage .= $this->jsonEncodeArray($record);
return $logMessage;
}

Wyświetl plik

@ -159,4 +159,23 @@ abstract class AbstractLoggerTest extends MockedTest
self::assertContains(@json_encode($context), $text);
}
/**
* Test a message with an exception
*/
public function testExceptionHandling()
{
$e = new \Exception("Test String", 123);
$eFollowUp = new \Exception("FollowUp", 456, $e);
$assertion = $eFollowUp->__toString();
$logger = $this->getInstance();
$logger->alert('test', ['e' => $eFollowUp]);
$text = $this->getContent();
self::assertLogline($text);
self::assertContains(@json_encode($assertion), $this->getContent());
}
}