Merge pull request #10101 from nupplaphil/fix/log_toString
Fix object handling inside log arrays
This commit is contained in:
commit
dffd07fb74
|
@ -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}
|
||||
*/
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user