1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-08-08 06:06:40 +02:00

Split off line formatter in normalizer + line formatters

This commit is contained in:
Jordi Boggiano
2012-04-22 12:34:41 +02:00
parent f5ee1cb25a
commit 0bd4d93006
6 changed files with 217 additions and 30 deletions

View File

@@ -5,6 +5,7 @@
* Added Monolog\Logger::isHandling() to check if a handler will * Added Monolog\Logger::isHandling() to check if a handler will
handle the given log level handle the given log level
* Added ChromePHPHandler * Added ChromePHPHandler
* Added NormalizerFormatter
* Added possibility to show microseconds in logs * Added possibility to show microseconds in logs
* Added `server` and `referer` to WebProcessor output * Added `server` and `referer` to WebProcessor output

View File

@@ -59,6 +59,7 @@ Formatters
---------- ----------
- _LineFormatter_: Formats a log record into a one-line string. - _LineFormatter_: Formats a log record into a one-line string.
- _NormalizerFormatter_: Normalizes objects/resources down to strings so a record can easily be serialized/encoded.
- _JsonFormatter_: Encodes a log record into json. - _JsonFormatter_: Encodes a log record into json.
- _WildfireFormatter_: Used to format log records into the Wildfire/FirePHP protocol, only useful for the FirePHPHandler. - _WildfireFormatter_: Used to format log records into the Wildfire/FirePHP protocol, only useful for the FirePHPHandler.
- _ChromePHPFormatter_: Used to format log records into the ChromePHP format, only useful for the ChromePHPHandler. - _ChromePHPFormatter_: Used to format log records into the ChromePHP format, only useful for the ChromePHPHandler.

View File

@@ -21,13 +21,11 @@ use Monolog\Logger;
* @author Jordi Boggiano <j.boggiano@seld.be> * @author Jordi Boggiano <j.boggiano@seld.be>
* @author Christophe Coevoet <stof@notk.org> * @author Christophe Coevoet <stof@notk.org>
*/ */
class LineFormatter implements FormatterInterface class LineFormatter extends NormalizerFormatter
{ {
const SIMPLE_FORMAT = "[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n"; const SIMPLE_FORMAT = "[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n";
const SIMPLE_DATE = "Y-m-d H:i:s";
protected $format; protected $format;
protected $dateFormat;
/** /**
* @param string $format The format of the message * @param string $format The format of the message
@@ -36,7 +34,7 @@ class LineFormatter implements FormatterInterface
public function __construct($format = null, $dateFormat = null) public function __construct($format = null, $dateFormat = null)
{ {
$this->format = $format ?: static::SIMPLE_FORMAT; $this->format = $format ?: static::SIMPLE_FORMAT;
$this->dateFormat = $dateFormat ?: static::SIMPLE_DATE; parent::__construct($dateFormat);
} }
/** /**
@@ -44,8 +42,7 @@ class LineFormatter implements FormatterInterface
*/ */
public function format(array $record) public function format(array $record)
{ {
$vars = $record; $vars = parent::format($record);
$vars['datetime'] = $vars['datetime']->format($this->dateFormat);
$output = $this->format; $output = $this->format;
foreach ($vars['extra'] as $var => $val) { foreach ($vars['extra'] as $var => $val) {
@@ -71,35 +68,25 @@ class LineFormatter implements FormatterInterface
return $message; return $message;
} }
protected function normalize($data)
{
if (is_bool($data) || is_null($data)) {
return var_export($data, true);
}
return parent::normalize($data);
}
protected function convertToString($data) protected function convertToString($data)
{ {
if (null === $data || is_scalar($data)) { if (null === $data || is_scalar($data)) {
return (string) $data; return (string) $data;
} }
if (version_compare(PHP_VERSION, '5.4.0', '>=')) {
return json_encode($this->normalize($data), JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
}
return stripslashes(json_encode($this->normalize($data))); return stripslashes(json_encode($this->normalize($data)));
} }
protected function normalize($data)
{
if (null === $data || is_scalar($data)) {
return $data;
}
if (is_array($data) || $data instanceof \Traversable) {
$normalized = array();
foreach ($data as $key => $value) {
$normalized[$key] = $this->normalize($value);
}
return $normalized;
}
if (is_resource($data)) {
return '[resource]';
}
return sprintf("[object] (%s: %s)", get_class($data), json_encode($data));
}
} }

View File

@@ -0,0 +1,87 @@
<?php
/*
* This file is part of the Monolog package.
*
* (c) Jordi Boggiano <j.boggiano@seld.be>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Monolog\Formatter;
use Monolog\Logger;
/**
* Normalizes incoming records to remove objects/resources so it's easier to dump to various targets
*
* @author Jordi Boggiano <j.boggiano@seld.be>
*/
class NormalizerFormatter implements FormatterInterface
{
const SIMPLE_DATE = "Y-m-d H:i:s";
protected $dateFormat;
/**
* @param string $dateFormat The format of the timestamp: one supported by DateTime::format
*/
public function __construct($dateFormat = null)
{
$this->dateFormat = $dateFormat ?: static::SIMPLE_DATE;
}
/**
* {@inheritdoc}
*/
public function format(array $record)
{
return $this->normalize($record);
}
/**
* {@inheritdoc}
*/
public function formatBatch(array $records)
{
foreach ($records as $key => $record) {
$records[$key] = $this->format($record);
}
return $records;
}
protected function normalize($data)
{
if (null === $data || is_scalar($data)) {
return $data;
}
if (is_array($data) || $data instanceof \Traversable) {
$normalized = array();
foreach ($data as $key => $value) {
$normalized[$key] = $this->normalize($value);
}
return $normalized;
}
if ($data instanceof \DateTime) {
return $data->format($this->dateFormat);
}
if (is_resource($data)) {
return '[resource]';
}
if (version_compare(PHP_VERSION, '5.4.0', '>=')) {
$encoded = json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
} else {
$encoded = json_encode($data);
}
return sprintf("[object] (%s: %s)", get_class($data), $encoded);
}
}

View File

@@ -88,7 +88,11 @@ class LineFormatterTest extends \PHPUnit_Framework_TestCase
'extra' => array('foo' => new TestFoo, 'bar' => new TestBar, 'baz' => array(), 'res' => fopen('php://memory', 'rb')), 'extra' => array('foo' => new TestFoo, 'bar' => new TestBar, 'baz' => array(), 'res' => fopen('php://memory', 'rb')),
'message' => 'foobar', 'message' => 'foobar',
)); ));
$this->assertEquals('['.date('Y-m-d').'] meh.ERROR: foobar [] {"foo":"[object] (Monolog\\Formatter\\TestFoo: {"foo":"foo"})","bar":"[object] (Monolog\\Formatter\\TestBar: {})","baz":[],"res":"[resource]"}'."\n", $message); if (version_compare(PHP_VERSION, '5.4.0', '>=')) {
$this->assertEquals('['.date('Y-m-d').'] meh.ERROR: foobar [] {"foo":"[object] (Monolog\\\\Formatter\\\\TestFoo: {\\"foo\\":\\"foo\\"})","bar":"[object] (Monolog\\\\Formatter\\\\TestBar: {})","baz":[],"res":"[resource]"}'."\n", $message);
} else {
$this->assertEquals('['.date('Y-m-d').'] meh.ERROR: foobar [] {"foo":"[object] (Monolog\\Formatter\\TestFoo: {"foo":"foo"})","bar":"[object] (Monolog\\Formatter\\TestBar: {})","baz":[],"res":"[resource]"}'."\n", $message);
}
} }
public function testBatchFormat() public function testBatchFormat()

View File

@@ -0,0 +1,107 @@
<?php
/*
* This file is part of the Monolog package.
*
* (c) Jordi Boggiano <j.boggiano@seld.be>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Monolog\Formatter;
use Monolog\Logger;
/**
* @covers Monolog\Formatter\NormalizerFormatter
*/
class NormalizerFormatterTest extends \PHPUnit_Framework_TestCase
{
public function testFormat()
{
$formatter = new NormalizerFormatter('Y-m-d');
$formatted = $formatter->format(array(
'level_name' => 'ERROR',
'channel' => 'meh',
'message' => 'foo',
'datetime' => new \DateTime,
'extra' => array('foo' => new TestFooNorm, 'bar' => new TestBarNorm, 'baz' => array(), 'res' => fopen('php://memory', 'rb')),
'context' => array(
'foo' => 'bar',
'baz' => 'qux',
)
));
$this->assertEquals(array(
'level_name' => 'ERROR',
'channel' => 'meh',
'message' => 'foo',
'datetime' => date('Y-m-d'),
'extra' => array(
'foo' => '[object] (Monolog\\Formatter\\TestFooNorm: {"foo":"foo"})',
'bar' => '[object] (Monolog\\Formatter\\TestBarNorm: {})',
'baz' => array(),
'res' => '[resource]',
),
'context' => array(
'foo' => 'bar',
'baz' => 'qux',
)
), $formatted);
}
public function testBatchFormat()
{
$formatter = new NormalizerFormatter('Y-m-d');
$formatted = $formatter->formatBatch(array(
array(
'level_name' => 'CRITICAL',
'channel' => 'test',
'message' => 'bar',
'context' => array(),
'datetime' => new \DateTime,
'extra' => array(),
),
array(
'level_name' => 'WARNING',
'channel' => 'log',
'message' => 'foo',
'context' => array(),
'datetime' => new \DateTime,
'extra' => array(),
),
));
$this->assertEquals(array(
array(
'level_name' => 'CRITICAL',
'channel' => 'test',
'message' => 'bar',
'context' => array(),
'datetime' => date('Y-m-d'),
'extra' => array(),
),
array(
'level_name' => 'WARNING',
'channel' => 'log',
'message' => 'foo',
'context' => array(),
'datetime' => date('Y-m-d'),
'extra' => array(),
),
), $formatted);
}
}
class TestFooNorm
{
public $foo = 'foo';
}
class TestBarNorm
{
public function __toString()
{
return 'bar';
}
}