1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-10-22 17:16:18 +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
{
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);
}
}