summaryrefslogtreecommitdiffstats
path: root/src/Monolog/Formatter/JsonFormatter.php
diff options
context:
space:
mode:
Diffstat (limited to 'src/Monolog/Formatter/JsonFormatter.php')
-rw-r--r--src/Monolog/Formatter/JsonFormatter.php67
1 files changed, 22 insertions, 45 deletions
diff --git a/src/Monolog/Formatter/JsonFormatter.php b/src/Monolog/Formatter/JsonFormatter.php
index 0782f14..8e2f2fd 100644
--- a/src/Monolog/Formatter/JsonFormatter.php
+++ b/src/Monolog/Formatter/JsonFormatter.php
@@ -1,4 +1,4 @@
-<?php
+<?php declare(strict_types=1);
/*
* This file is part of the Monolog package.
@@ -11,7 +11,6 @@
namespace Monolog\Formatter;
-use Exception;
use Throwable;
/**
@@ -34,11 +33,7 @@ class JsonFormatter extends NormalizerFormatter
*/
protected $includeStacktraces = false;
- /**
- * @param int $batchMode
- * @param bool $appendNewline
- */
- public function __construct($batchMode = self::BATCH_MODE_JSON, $appendNewline = true)
+ public function __construct(int $batchMode = self::BATCH_MODE_JSON, bool $appendNewline = true)
{
$this->batchMode = $batchMode;
$this->appendNewline = $appendNewline;
@@ -50,20 +45,16 @@ class JsonFormatter extends NormalizerFormatter
* formatted as a JSON-encoded array. However, for
* compatibility with some API endpoints, alternative styles
* are available.
- *
- * @return int
*/
- public function getBatchMode()
+ public function getBatchMode(): int
{
return $this->batchMode;
}
/**
* True if newlines are appended to every formatted record
- *
- * @return bool
*/
- public function isAppendingNewlines()
+ public function isAppendingNewlines(): bool
{
return $this->appendNewline;
}
@@ -71,7 +62,7 @@ class JsonFormatter extends NormalizerFormatter
/**
* {@inheritdoc}
*/
- public function format(array $record)
+ public function format(array $record): string
{
return $this->toJson($this->normalize($record), true) . ($this->appendNewline ? "\n" : '');
}
@@ -79,7 +70,7 @@ class JsonFormatter extends NormalizerFormatter
/**
* {@inheritdoc}
*/
- public function formatBatch(array $records)
+ public function formatBatch(array $records): string
{
switch ($this->batchMode) {
case static::BATCH_MODE_NEWLINES:
@@ -91,21 +82,15 @@ class JsonFormatter extends NormalizerFormatter
}
}
- /**
- * @param bool $include
- */
- public function includeStacktraces($include = true)
+ public function includeStacktraces(bool $include = true)
{
$this->includeStacktraces = $include;
}
/**
* Return a JSON-encoded array of records.
- *
- * @param array $records
- * @return string
*/
- protected function formatBatchJson(array $records)
+ protected function formatBatchJson(array $records): string
{
return $this->toJson($this->normalize($records), true);
}
@@ -113,11 +98,8 @@ class JsonFormatter extends NormalizerFormatter
/**
* Use new lines to separate records instead of a
* JSON-encoded array.
- *
- * @param array $records
- * @return string
*/
- protected function formatBatchNewlines(array $records)
+ protected function formatBatchNewlines(array $records): string
{
$instance = $this;
@@ -138,10 +120,14 @@ class JsonFormatter extends NormalizerFormatter
*
* @return mixed
*/
- protected function normalize($data)
+ protected function normalize($data, int $depth = 0)
{
+ if ($depth > 9) {
+ return 'Over 9 levels deep, aborting normalization';
+ }
+
if (is_array($data) || $data instanceof \Traversable) {
- $normalized = array();
+ $normalized = [];
$count = 1;
foreach ($data as $key => $value) {
@@ -149,14 +135,14 @@ class JsonFormatter extends NormalizerFormatter
$normalized['...'] = 'Over 1000 items, aborting normalization';
break;
}
- $normalized[$key] = $this->normalize($value);
+ $normalized[$key] = $this->normalize($value, $depth + 1);
}
return $normalized;
}
- if ($data instanceof Exception || $data instanceof Throwable) {
- return $this->normalizeException($data);
+ if ($data instanceof Throwable) {
+ return $this->normalizeException($data, $depth);
}
return $data;
@@ -165,24 +151,15 @@ class JsonFormatter extends NormalizerFormatter
/**
* Normalizes given exception with or without its own stack trace based on
* `includeStacktraces` property.
- *
- * @param Exception|Throwable $e
- *
- * @return array
*/
- protected function normalizeException($e)
+ protected function normalizeException(Throwable $e, int $depth = 0): array
{
- // TODO 2.0 only check for Throwable
- if (!$e instanceof Exception && !$e instanceof Throwable) {
- throw new \InvalidArgumentException('Exception/Throwable expected, got '.gettype($e).' / '.get_class($e));
- }
-
- $data = array(
+ $data = [
'class' => get_class($e),
'message' => $e->getMessage(),
'code' => $e->getCode(),
'file' => $e->getFile().':'.$e->getLine(),
- );
+ ];
if ($this->includeStacktraces) {
$trace = $e->getTrace();
@@ -200,7 +177,7 @@ class JsonFormatter extends NormalizerFormatter
}
if ($previous = $e->getPrevious()) {
- $data['previous'] = $this->normalizeException($previous);
+ $data['previous'] = $this->normalizeException($previous, $depth + 1);
}
return $data;