1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-08-04 20:27:31 +02:00

Refactor scalar formatter

Removed some of its own logic in favor of extending NormalizerFormatter to
benefit from shared logic.
This commit is contained in:
adlawson
2013-11-03 19:47:29 +00:00
parent 471bc7fa91
commit ac091bffcd
2 changed files with 36 additions and 97 deletions

View File

@@ -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 <adlawson@gmail.com>
*/
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;
}
}

View File

@@ -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);