diff options
Diffstat (limited to 'src/Monolog/Formatter/NormalizerFormatter.php')
-rw-r--r-- | src/Monolog/Formatter/NormalizerFormatter.php | 50 |
1 files changed, 38 insertions, 12 deletions
diff --git a/src/Monolog/Formatter/NormalizerFormatter.php b/src/Monolog/Formatter/NormalizerFormatter.php index fcac4a6..592713f 100644 --- a/src/Monolog/Formatter/NormalizerFormatter.php +++ b/src/Monolog/Formatter/NormalizerFormatter.php @@ -39,7 +39,7 @@ class NormalizerFormatter implements FormatterInterface /** * {@inheritdoc} */ - public function format(array $record) + public function format(array $record): array { return $this->normalize($record); } @@ -47,7 +47,7 @@ class NormalizerFormatter implements FormatterInterface /** * {@inheritdoc} */ - public function formatBatch(array $records) + public function formatBatch(array $records): array { foreach ($records as $key => $record) { $records[$key] = $this->format($record); @@ -56,7 +56,11 @@ class NormalizerFormatter implements FormatterInterface return $records; } - protected function normalize($data, $depth = 0) + /** + * @param mixed $data + * @return int|bool|string|null|array + */ + protected function normalize($data, int $depth = 0) { if ($depth > 9) { return 'Over 9 levels deep, aborting normalization'; @@ -79,9 +83,13 @@ class NormalizerFormatter implements FormatterInterface $normalized = []; $count = 1; + if ($data instanceof \Generator && !$data->valid()) { + return array('...' => 'Generator is already consumed, aborting'); + } + foreach ($data as $key => $value) { if ($count++ >= 1000) { - $normalized['...'] = 'Over 1000 items, aborting normalization'; + $normalized['...'] = 'Over 1000 items ('.($data instanceof \Generator ? 'generator function' : count($data).' total').'), aborting normalization'; break; } $normalized[$key] = $this->normalize($value, $depth + 1); @@ -96,7 +104,7 @@ class NormalizerFormatter implements FormatterInterface if (is_object($data)) { if ($data instanceof Throwable) { - return $this->normalizeException($data); + return $this->normalizeException($data, $depth); } if ($data instanceof \JsonSerializable) { @@ -123,7 +131,10 @@ class NormalizerFormatter implements FormatterInterface return '[unknown('.gettype($data).')]'; } - protected function normalizeException(Throwable $e) + /** + * @return array + */ + protected function normalizeException(Throwable $e, int $depth = 0) { $data = [ 'class' => get_class($e), @@ -132,6 +143,20 @@ class NormalizerFormatter implements FormatterInterface 'file' => $e->getFile().':'.$e->getLine(), ]; + if ($e instanceof \SoapFault) { + if (isset($e->faultcode)) { + $data['faultcode'] = $e->faultcode; + } + + if (isset($e->faultactor)) { + $data['faultactor'] = $e->faultactor; + } + + if (isset($e->detail)) { + $data['detail'] = $e->detail; + } + } + $trace = $e->getTrace(); foreach ($trace as $frame) { if (isset($frame['file'])) { @@ -141,12 +166,12 @@ class NormalizerFormatter implements FormatterInterface $data['trace'][] = $frame['function']; } else { // We should again normalize the frames, because it might contain invalid items - $data['trace'][] = $this->toJson($this->normalize($frame), true); + $data['trace'][] = $this->toJson($this->normalize($frame, $depth + 1), true); } } if ($previous = $e->getPrevious()) { - $data['previous'] = $this->normalizeException($previous); + $data['previous'] = $this->normalizeException($previous, $depth + 1); } return $data; @@ -156,11 +181,10 @@ class NormalizerFormatter implements FormatterInterface * Return the JSON representation of a value * * @param mixed $data - * @param bool $ignoreErrors * @throws \RuntimeException if encoding fails and errors are not ignored * @return string|bool */ - protected function toJson($data, $ignoreErrors = false) + protected function toJson($data, bool $ignoreErrors = false) { // suppress json_encode errors since it's twitchy with some inputs if ($ignoreErrors) { @@ -182,7 +206,7 @@ class NormalizerFormatter implements FormatterInterface */ private function jsonEncode($data) { - return json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); + return json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRESERVE_ZERO_FRACTION); } /** @@ -286,7 +310,9 @@ class NormalizerFormatter implements FormatterInterface protected function formatDate(\DateTimeInterface $date) { - if ($date instanceof DateTimeImmutable) { + // in case the date format isn't custom then we defer to the custom DateTimeImmutable + // formatting logic, which will pick the right format based on whether useMicroseconds is on + if ($this->dateFormat === self::SIMPLE_DATE && $date instanceof DateTimeImmutable) { return (string) $date; } |