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

Added $appendNewline flag to the JsonFormatter to enable using it when logging to files, fixes #344

This commit is contained in:
Jordi Boggiano
2014-04-20 18:38:47 +02:00
parent 53d0df17bb
commit 5e8bb7556b
5 changed files with 36 additions and 20 deletions

View File

@@ -20,18 +20,19 @@ namespace Monolog\Formatter;
*/ */
class JsonFormatter implements FormatterInterface class JsonFormatter implements FormatterInterface
{ {
protected $batchMode;
protected $batch_mode; protected $appendNewline;
const BATCH_MODE_JSON = 1; const BATCH_MODE_JSON = 1;
const BATCH_MODE_NEWLINES = 2; const BATCH_MODE_NEWLINES = 2;
/** /**
* @param int $batch_mode * @param int $batchMode
*/ */
public function __construct($batch_mode = self::BATCH_MODE_JSON) public function __construct($batchMode = self::BATCH_MODE_JSON, $appendNewline = true)
{ {
$this->batch_mode = $batch_mode; $this->batchMode = $batchMode;
$this->appendNewline = $appendNewline;
} }
/** /**
@@ -45,7 +46,17 @@ class JsonFormatter implements FormatterInterface
*/ */
public function getBatchMode() public function getBatchMode()
{ {
return $this->batch_mode; return $this->batchMode;
}
/**
* True if newlines are appended to every formatted record
*
* @return bool
*/
public function isAppendingNewlines()
{
return $this->appendNewline;
} }
/** /**
@@ -53,7 +64,7 @@ class JsonFormatter implements FormatterInterface
*/ */
public function format(array $record) public function format(array $record)
{ {
return json_encode($record); return json_encode($record) . ($this->appendNewline ? "\n" : '');
} }
/** /**
@@ -61,15 +72,13 @@ class JsonFormatter implements FormatterInterface
*/ */
public function formatBatch(array $records) public function formatBatch(array $records)
{ {
switch ($this->batch_mode) { switch ($this->batchMode) {
case static::BATCH_MODE_NEWLINES: case static::BATCH_MODE_NEWLINES:
return $this->formatBatchNewlines($records); return $this->formatBatchNewlines($records);
case static::BATCH_MODE_JSON: case static::BATCH_MODE_JSON:
default: default:
return $this->formatBatchJson($records); return $this->formatBatchJson($records);
} }
} }
@@ -95,11 +104,13 @@ class JsonFormatter implements FormatterInterface
{ {
$instance = $this; $instance = $this;
$oldNewline = $this->appendNewline;
$this->appendNewline = false;
array_walk($records, function (&$value, $key) use ($instance) { array_walk($records, function (&$value, $key) use ($instance) {
$value = $instance->format($value); $value = $instance->format($value);
}); });
$this->appendNewline = $oldNewline;
return implode("\n", $records); return implode("\n", $records);
} }
} }

View File

@@ -18,16 +18,15 @@ namespace Monolog\Formatter;
*/ */
class LogglyFormatter extends JsonFormatter class LogglyFormatter extends JsonFormatter
{ {
/** /**
* Overrides the default batch mode to new lines for compatibility with the * Overrides the default batch mode to new lines for compatibility with the
* Loggly bulk API. * Loggly bulk API.
* *
* @param integer $batch_mode * @param integer $batchMode
*/ */
public function __construct($batch_mode = self::BATCH_MODE_NEWLINES) public function __construct($batchMode = self::BATCH_MODE_NEWLINES, $appendNewline = false)
{ {
parent::__construct($batch_mode); parent::__construct($batchMode, $appendNewline);
} }
/** /**
@@ -40,10 +39,9 @@ class LogglyFormatter extends JsonFormatter
{ {
if (isset($record["datetime"]) && ($record["datetime"] instanceof \DateTime)) { if (isset($record["datetime"]) && ($record["datetime"] instanceof \DateTime)) {
$record["timestamp"] = $record["datetime"]->format("c"); $record["timestamp"] = $record["datetime"]->format("c");
// @todo unset the 'datetime' parameter, retained for BC // TODO 2.0 unset the 'datetime' parameter, retained for BC
} }
return parent::format($record); return parent::format($record);
} }
} }

View File

@@ -64,6 +64,6 @@ class AmqpHandler extends AbstractProcessingHandler
*/ */
protected function getDefaultFormatter() protected function getDefaultFormatter()
{ {
return new JsonFormatter(); return new JsonFormatter(JsonFormatter::BATCH_MODE_JSON, false);
} }
} }

View File

@@ -67,6 +67,6 @@ class CouchDBHandler extends AbstractProcessingHandler
*/ */
protected function getDefaultFormatter() protected function getDefaultFormatter()
{ {
return new JsonFormatter(); return new JsonFormatter(JsonFormatter::BATCH_MODE_JSON, false);
} }
} }

View File

@@ -19,13 +19,16 @@ class JsonFormatterTest extends TestCase
/** /**
* @covers Monolog\Formatter\JsonFormatter::__construct * @covers Monolog\Formatter\JsonFormatter::__construct
* @covers Monolog\Formatter\JsonFormatter::getBatchMode * @covers Monolog\Formatter\JsonFormatter::getBatchMode
* @covers Monolog\Formatter\JsonFormatter::isAppendingNewlines
*/ */
public function testConstruct() public function testConstruct()
{ {
$formatter = new JsonFormatter(); $formatter = new JsonFormatter();
$this->assertEquals(JsonFormatter::BATCH_MODE_JSON, $formatter->getBatchMode()); $this->assertEquals(JsonFormatter::BATCH_MODE_JSON, $formatter->getBatchMode());
$formatter = new JsonFormatter(JsonFormatter::BATCH_MODE_NEWLINES); $this->assertEquals(true, $formatter->isAppendingNewlines());
$formatter = new JsonFormatter(JsonFormatter::BATCH_MODE_NEWLINES, false);
$this->assertEquals(JsonFormatter::BATCH_MODE_NEWLINES, $formatter->getBatchMode()); $this->assertEquals(JsonFormatter::BATCH_MODE_NEWLINES, $formatter->getBatchMode());
$this->assertEquals(false, $formatter->isAppendingNewlines());
} }
/** /**
@@ -35,6 +38,10 @@ class JsonFormatterTest extends TestCase
{ {
$formatter = new JsonFormatter(); $formatter = new JsonFormatter();
$record = $this->getRecord(); $record = $this->getRecord();
$this->assertEquals(json_encode($record)."\n", $formatter->format($record));
$formatter = new JsonFormatter(JsonFormatter::BATCH_MODE_JSON, false);
$record = $this->getRecord();
$this->assertEquals(json_encode($record), $formatter->format($record)); $this->assertEquals(json_encode($record), $formatter->format($record));
} }