mirror of
https://github.com/Seldaek/monolog.git
synced 2025-08-03 19:57:41 +02:00
Changed the FormatterInterface to return only the formatted message and added a batch formatting method
This commit is contained in:
@@ -22,7 +22,15 @@ interface FormatterInterface
|
|||||||
* Formats a log record.
|
* Formats a log record.
|
||||||
*
|
*
|
||||||
* @param array $record A record to format
|
* @param array $record A record to format
|
||||||
* @return array The record with a formatted message
|
* @return string The formatted message
|
||||||
*/
|
*/
|
||||||
function format(array $record);
|
function format(array $record);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Formats a set of log records.
|
||||||
|
*
|
||||||
|
* @param array $record A record to format
|
||||||
|
* @return string The formatted batch message
|
||||||
|
*/
|
||||||
|
function formatBatch(array $records);
|
||||||
}
|
}
|
||||||
|
@@ -27,8 +27,14 @@ class JsonFormatter implements FormatterInterface
|
|||||||
*/
|
*/
|
||||||
public function format(array $record)
|
public function format(array $record)
|
||||||
{
|
{
|
||||||
$record['message'] = json_encode($record);
|
return json_encode($record);
|
||||||
|
}
|
||||||
|
|
||||||
return $record;
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function formatBatch(array $records)
|
||||||
|
{
|
||||||
|
return json_encode($records);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -62,8 +62,17 @@ class LineFormatter implements FormatterInterface
|
|||||||
foreach ($vars['extra'] as $var => $val) {
|
foreach ($vars['extra'] as $var => $val) {
|
||||||
$output = str_replace('%extra.'.$var.'%', $val, $output);
|
$output = str_replace('%extra.'.$var.'%', $val, $output);
|
||||||
}
|
}
|
||||||
$record['message'] = $output;
|
|
||||||
|
|
||||||
return $record;
|
return $output;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function formatBatch(array $records)
|
||||||
|
{
|
||||||
|
$message = '';
|
||||||
|
foreach ($records as $record) {
|
||||||
|
$message .= $this->format($record);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $message;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -18,7 +18,7 @@ use Monolog\Logger;
|
|||||||
*
|
*
|
||||||
* @author Eric Clemmons (@ericclemmons) <eric@uxdriven.com>
|
* @author Eric Clemmons (@ericclemmons) <eric@uxdriven.com>
|
||||||
*/
|
*/
|
||||||
class WildfireFormatter extends LineFormatter implements FormatterInterface
|
class WildfireFormatter extends LineFormatter
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Similar to LineFormatter::SIMPLE_FORMAT, except without the "[%datetime%]"
|
* Similar to LineFormatter::SIMPLE_FORMAT, except without the "[%datetime%]"
|
||||||
@@ -43,7 +43,7 @@ class WildfireFormatter extends LineFormatter implements FormatterInterface
|
|||||||
public function format(array $record)
|
public function format(array $record)
|
||||||
{
|
{
|
||||||
// Format record according with LineFormatter
|
// Format record according with LineFormatter
|
||||||
$formatted = parent::format($record);
|
$message = parent::format($record);
|
||||||
|
|
||||||
// Create JSON object describing the appearance of the message in the console
|
// Create JSON object describing the appearance of the message in the console
|
||||||
$json = json_encode(array(
|
$json = json_encode(array(
|
||||||
@@ -52,17 +52,19 @@ class WildfireFormatter extends LineFormatter implements FormatterInterface
|
|||||||
'File' => '',
|
'File' => '',
|
||||||
'Line' => '',
|
'Line' => '',
|
||||||
),
|
),
|
||||||
$formatted['message'],
|
$message,
|
||||||
));
|
));
|
||||||
|
|
||||||
// The message itself is a serialization of the above JSON object + it's length
|
// The message itself is a serialization of the above JSON object + it's length
|
||||||
$formatted['message'] = sprintf(
|
return sprintf(
|
||||||
'%s|%s|',
|
'%s|%s|',
|
||||||
strlen($json),
|
strlen($json),
|
||||||
$json
|
$json
|
||||||
);
|
);
|
||||||
|
|
||||||
return $formatted;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function formatBatch(array $records)
|
||||||
|
{
|
||||||
|
throw new \BadMethodCallException('Batch formatting does not make sense for the WildfireFormatter');
|
||||||
|
}
|
||||||
}
|
}
|
@@ -27,6 +27,9 @@ abstract class AbstractHandler implements HandlerInterface
|
|||||||
protected $level = Logger::DEBUG;
|
protected $level = Logger::DEBUG;
|
||||||
protected $bubble = false;
|
protected $bubble = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var FormatterInterface
|
||||||
|
*/
|
||||||
protected $formatter;
|
protected $formatter;
|
||||||
protected $processors = array();
|
protected $processors = array();
|
||||||
|
|
||||||
@@ -57,16 +60,12 @@ abstract class AbstractHandler implements HandlerInterface
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($this->processors) {
|
$record = $this->processRecord($record);
|
||||||
foreach ($this->processors as $processor) {
|
|
||||||
$record = call_user_func($processor, $record);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!$this->formatter) {
|
if (!$this->formatter) {
|
||||||
$this->formatter = $this->getDefaultFormatter();
|
$this->formatter = $this->getDefaultFormatter();
|
||||||
}
|
}
|
||||||
$record = $this->formatter->format($record);
|
$record['message'] = $this->formatter->format($record);
|
||||||
|
|
||||||
$this->write($record);
|
$this->write($record);
|
||||||
|
|
||||||
@@ -188,4 +187,21 @@ abstract class AbstractHandler implements HandlerInterface
|
|||||||
{
|
{
|
||||||
return new LineFormatter();
|
return new LineFormatter();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Processes a record.
|
||||||
|
*
|
||||||
|
* @param array $record
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
protected function processRecord(array $record)
|
||||||
|
{
|
||||||
|
if ($this->processors) {
|
||||||
|
foreach ($this->processors as $processor) {
|
||||||
|
$record = call_user_func($processor, $record);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $record;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -18,13 +18,13 @@ class JsonFormatterTest extends \PHPUnit_Framework_TestCase
|
|||||||
public function testFormat()
|
public function testFormat()
|
||||||
{
|
{
|
||||||
$formatter = new JsonFormatter();
|
$formatter = new JsonFormatter();
|
||||||
$record = $formatter->format($msg = array(
|
$message = $formatter->format($msg = array(
|
||||||
'level_name' => 'WARNING',
|
'level_name' => 'WARNING',
|
||||||
'channel' => 'log',
|
'channel' => 'log',
|
||||||
'message' => array('foo'),
|
'message' => array('foo'),
|
||||||
'datetime' => new \DateTime,
|
'datetime' => new \DateTime,
|
||||||
'extra' => array(),
|
'extra' => array(),
|
||||||
));
|
));
|
||||||
$this->assertEquals(json_encode($msg), $record['message']);
|
$this->assertEquals(json_encode($msg), $message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -18,20 +18,20 @@ class LineFormatterTest extends \PHPUnit_Framework_TestCase
|
|||||||
public function testDefFormatWithString()
|
public function testDefFormatWithString()
|
||||||
{
|
{
|
||||||
$formatter = new LineFormatter(null, 'Y-m-d');
|
$formatter = new LineFormatter(null, 'Y-m-d');
|
||||||
$record = $formatter->format(array(
|
$message = $formatter->format(array(
|
||||||
'level_name' => 'WARNING',
|
'level_name' => 'WARNING',
|
||||||
'channel' => 'log',
|
'channel' => 'log',
|
||||||
'message' => 'foo',
|
'message' => 'foo',
|
||||||
'datetime' => new \DateTime,
|
'datetime' => new \DateTime,
|
||||||
'extra' => array(),
|
'extra' => array(),
|
||||||
));
|
));
|
||||||
$this->assertEquals('['.date('Y-m-d').'] log.WARNING: foo '."\n", $record['message']);
|
$this->assertEquals('['.date('Y-m-d').'] log.WARNING: foo '."\n", $message);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testDefFormatWithArray()
|
public function testDefFormatWithArray()
|
||||||
{
|
{
|
||||||
$formatter = new LineFormatter(null, 'Y-m-d');
|
$formatter = new LineFormatter(null, 'Y-m-d');
|
||||||
$record = $formatter->format(array(
|
$message = $formatter->format(array(
|
||||||
'level_name' => 'ERROR',
|
'level_name' => 'ERROR',
|
||||||
'channel' => 'meh',
|
'channel' => 'meh',
|
||||||
'datetime' => new \DateTime,
|
'datetime' => new \DateTime,
|
||||||
@@ -41,19 +41,19 @@ class LineFormatterTest extends \PHPUnit_Framework_TestCase
|
|||||||
'baz' => 'qux',
|
'baz' => 'qux',
|
||||||
)
|
)
|
||||||
));
|
));
|
||||||
$this->assertEquals('['.date('Y-m-d').'] meh.ERROR: message(foo: bar, baz: qux) '."\n", $record['message']);
|
$this->assertEquals('['.date('Y-m-d').'] meh.ERROR: message(foo: bar, baz: qux) '."\n", $message);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testDefFormatExtras()
|
public function testDefFormatExtras()
|
||||||
{
|
{
|
||||||
$formatter = new LineFormatter(null, 'Y-m-d');
|
$formatter = new LineFormatter(null, 'Y-m-d');
|
||||||
$record = $formatter->format(array(
|
$message = $formatter->format(array(
|
||||||
'level_name' => 'ERROR',
|
'level_name' => 'ERROR',
|
||||||
'channel' => 'meh',
|
'channel' => 'meh',
|
||||||
'datetime' => new \DateTime,
|
'datetime' => new \DateTime,
|
||||||
'extra' => array('ip' => '127.0.0.1'),
|
'extra' => array('ip' => '127.0.0.1'),
|
||||||
'message' => 'log',
|
'message' => 'log',
|
||||||
));
|
));
|
||||||
$this->assertEquals('['.date('Y-m-d').'] meh.ERROR: log extra(ip: 127.0.0.1)'."\n", $record['message']);
|
$this->assertEquals('['.date('Y-m-d').'] meh.ERROR: log extra(ip: 127.0.0.1)'."\n", $message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -22,11 +22,11 @@ class WildfireFormatterTest extends \PHPUnit_Framework_TestCase
|
|||||||
{
|
{
|
||||||
$wildfire = new WildfireFormatter();
|
$wildfire = new WildfireFormatter();
|
||||||
|
|
||||||
$record = $wildfire->format($record);
|
$message = $wildfire->format($record);
|
||||||
|
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
'70|[{"Type":"ERROR","File":"","Line":""},"meh: log extra(ip: 127.0.0.1)"]|',
|
'70|[{"Type":"ERROR","File":"","Line":""},"meh: log extra(ip: 127.0.0.1)"]|',
|
||||||
$record['message']
|
$message
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -36,7 +36,7 @@ class TestCase extends \PHPUnit_Framework_TestCase
|
|||||||
$formatter = $this->getMock('Monolog\\Formatter\\FormatterInterface');
|
$formatter = $this->getMock('Monolog\\Formatter\\FormatterInterface');
|
||||||
$formatter->expects($this->any())
|
$formatter->expects($this->any())
|
||||||
->method('format')
|
->method('format')
|
||||||
->will($this->returnArgument(0));
|
->will($this->returnCallback(function($record) { return $record['message']; }));
|
||||||
|
|
||||||
return $formatter;
|
return $formatter;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user