diff --git a/src/Monolog/Formatter/ScalarFormatter.php b/src/Monolog/Formatter/ScalarFormatter.php index 88be8e23..60075030 100644 --- a/src/Monolog/Formatter/ScalarFormatter.php +++ b/src/Monolog/Formatter/ScalarFormatter.php @@ -11,7 +11,7 @@ namespace Monolog\Formatter; -use Monolog\Formatter\FormatterInterface; +use Monolog\Formatter\NormalizerFormatter; /** * Formats data into an associative array of scalar values. @@ -19,107 +19,32 @@ use Monolog\Formatter\FormatterInterface; * * @author Andrew Lawson */ -class ScalarFormatter implements FormatterInterface +class ScalarFormatter extends NormalizerFormatter { - /** - * @var string - */ - protected $dateFormat; - - /** - * @param string $dateFormat - */ - public function __construct($dateFormat = \DateTime::ISO8601) - { - $this->dateFormat = $dateFormat; - } - /** * {@inheritdoc} */ public function format(array $record) { - $formatted = array(); - $record = $this->exposeContext($record); - foreach ($record as $key => $value) { - $formatted[$key] = $this->normalizeValue($value); - } - - return $formatted; - } - - /** - * {@inheritdoc} - */ - public function formatBatch(array $records) - { - $formatted = array(); - - foreach ($records as $record) { - $formatted[] = $this->format($record); - } - - return $formatted; - } - - /** - * @param mixed $data - * @return string - */ - protected function encodeData($data) - { - return json_encode($data, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT); - } - - /** - * @param array $record - * @return array - */ - protected function exposeContext(array $record) - { - if (isset($record['context'])) { - $context = $record['context']; - - if (isset($context['exception'])) { - $record['context']['exception'] = $this->normalizeException($context['exception']); - } + $record[$key] = $this->normalizeValue($value); } return $record; } - /** - * @param Exception $e - * @return string - */ - protected function normalizeException(\Exception $e) - { - return array( - 'message' => $e->getMessage(), - 'code' => $e->getCode(), - 'class' => get_class($e), - 'file' => $e->getFile(), - 'line' => $e->getLine(), - 'trace' => $e->getTraceAsString(), - 'debug' => $e->getTrace() - ); - } - /** * @param mixed $value * @return mixed */ protected function normalizeValue($value) { - if ($value instanceof \DateTime) { - return $value->format($this->dateFormat); - } elseif ($value instanceof \Exception) { - return $this->encodeData($this->normalizeException($value)); - } elseif (is_array($value) || is_object($value)) { - return $this->encodeData($value); + $normalized = $this->normalize($value); + + if (is_array($normalized) || is_object($normalized)) { + return $this->toJson($normalized, true); } - return $value; + return $normalized; } } diff --git a/tests/Monolog/Formatter/ScalarFormatterTest.php b/tests/Monolog/Formatter/ScalarFormatterTest.php index 7b8a5274..6320c89e 100644 --- a/tests/Monolog/Formatter/ScalarFormatterTest.php +++ b/tests/Monolog/Formatter/ScalarFormatterTest.php @@ -8,9 +8,29 @@ class ScalarFormatterTest extends \PHPUnit_Framework_TestCase $this->formatter = new ScalarFormatter(); } + public function buildTrace(\Exception $e) + { + $data = array(); + $trace = $e->getTrace(); + array_shift($trace); + foreach ($trace as $frame) { + if (isset($frame['file'])) { + $data[] = $frame['file'].':'.$frame['line']; + } else { + $data[] = json_encode($frame); + } + } + + return $data; + } + public function encodeJson($data) { - return json_encode($data, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT); + if (version_compare(PHP_VERSION, '5.4.0', '>=')) { + return json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); + } + + return json_encode($data); } public function testFormat() @@ -32,15 +52,12 @@ class ScalarFormatterTest extends \PHPUnit_Framework_TestCase 'baz' => false, 'bam' => $this->encodeJson(array(1,2,3)), 'bat' => $this->encodeJson(array('foo' => 'bar')), - 'bap' => '1970-01-01T00:00:00+0000', + 'bap' => '1970-01-01 00:00:00', 'ban' => $this->encodeJson(array( - 'message' => $exception->getMessage(), - 'code' => $exception->getCode(), 'class' => get_class($exception), - 'file' => $exception->getFile(), - 'line' => $exception->getLine(), - 'trace' => $exception->getTraceAsString(), - 'debug' => $exception->getTrace() + 'message' => $exception->getMessage(), + 'file' => $exception->getFile() . ':' . $exception->getLine(), + 'trace' => $this->buildTrace($exception) )) ), $formatted); } @@ -69,13 +86,10 @@ class ScalarFormatterTest extends \PHPUnit_Framework_TestCase $this->assertSame(array( 'context' => $this->encodeJson(array( 'exception' => array( - 'message' => $exception->getMessage(), - 'code' => $exception->getCode(), 'class' => get_class($exception), - 'file' => $exception->getFile(), - 'line' => $exception->getLine(), - 'trace' => $exception->getTraceAsString(), - 'debug' => $exception->getTrace() + 'message' => $exception->getMessage(), + 'file' => $exception->getFile() . ':' . $exception->getLine(), + 'trace' => $this->buildTrace($exception) ) )) ), $formatted);