mirror of
https://github.com/Seldaek/monolog.git
synced 2025-08-05 20:57:36 +02:00
Added $appendNewline flag to the JsonFormatter to enable using it when logging to files, fixes #344
This commit is contained in:
@@ -20,18 +20,19 @@ namespace Monolog\Formatter;
|
||||
*/
|
||||
class JsonFormatter implements FormatterInterface
|
||||
{
|
||||
|
||||
protected $batch_mode;
|
||||
protected $batchMode;
|
||||
protected $appendNewline;
|
||||
|
||||
const BATCH_MODE_JSON = 1;
|
||||
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()
|
||||
{
|
||||
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)
|
||||
{
|
||||
return json_encode($record);
|
||||
return json_encode($record) . ($this->appendNewline ? "\n" : '');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -61,15 +72,13 @@ class JsonFormatter implements FormatterInterface
|
||||
*/
|
||||
public function formatBatch(array $records)
|
||||
{
|
||||
switch ($this->batch_mode) {
|
||||
|
||||
switch ($this->batchMode) {
|
||||
case static::BATCH_MODE_NEWLINES:
|
||||
return $this->formatBatchNewlines($records);
|
||||
|
||||
case static::BATCH_MODE_JSON:
|
||||
default:
|
||||
return $this->formatBatchJson($records);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -95,11 +104,13 @@ class JsonFormatter implements FormatterInterface
|
||||
{
|
||||
$instance = $this;
|
||||
|
||||
$oldNewline = $this->appendNewline;
|
||||
$this->appendNewline = false;
|
||||
array_walk($records, function (&$value, $key) use ($instance) {
|
||||
$value = $instance->format($value);
|
||||
});
|
||||
$this->appendNewline = $oldNewline;
|
||||
|
||||
return implode("\n", $records);
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -18,16 +18,15 @@ namespace Monolog\Formatter;
|
||||
*/
|
||||
class LogglyFormatter extends JsonFormatter
|
||||
{
|
||||
|
||||
/**
|
||||
* Overrides the default batch mode to new lines for compatibility with the
|
||||
* 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)) {
|
||||
$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);
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -64,6 +64,6 @@ class AmqpHandler extends AbstractProcessingHandler
|
||||
*/
|
||||
protected function getDefaultFormatter()
|
||||
{
|
||||
return new JsonFormatter();
|
||||
return new JsonFormatter(JsonFormatter::BATCH_MODE_JSON, false);
|
||||
}
|
||||
}
|
||||
|
@@ -67,6 +67,6 @@ class CouchDBHandler extends AbstractProcessingHandler
|
||||
*/
|
||||
protected function getDefaultFormatter()
|
||||
{
|
||||
return new JsonFormatter();
|
||||
return new JsonFormatter(JsonFormatter::BATCH_MODE_JSON, false);
|
||||
}
|
||||
}
|
||||
|
@@ -19,13 +19,16 @@ class JsonFormatterTest extends TestCase
|
||||
/**
|
||||
* @covers Monolog\Formatter\JsonFormatter::__construct
|
||||
* @covers Monolog\Formatter\JsonFormatter::getBatchMode
|
||||
* @covers Monolog\Formatter\JsonFormatter::isAppendingNewlines
|
||||
*/
|
||||
public function testConstruct()
|
||||
{
|
||||
$formatter = new JsonFormatter();
|
||||
$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(false, $formatter->isAppendingNewlines());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -35,6 +38,10 @@ class JsonFormatterTest extends TestCase
|
||||
{
|
||||
$formatter = new JsonFormatter();
|
||||
$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));
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user