mirror of
https://github.com/Seldaek/monolog.git
synced 2025-10-23 01:26:11 +02:00
Replaced LogglyFormatter with option to change batch formatting in JsonFormatter
This commit is contained in:
@@ -20,6 +20,34 @@ namespace Monolog\Formatter;
|
||||
*/
|
||||
class JsonFormatter implements FormatterInterface
|
||||
{
|
||||
|
||||
protected $batch_mode;
|
||||
|
||||
const BATCH_MODE_JSON = 1;
|
||||
const BATCH_MODE_NEWLINES = 2;
|
||||
|
||||
/**
|
||||
* @param int $batch_mode
|
||||
*/
|
||||
public function __construct($batch_mode = self::BATCH_MODE_JSON)
|
||||
{
|
||||
$this->batch_mode = $batch_mode;
|
||||
}
|
||||
|
||||
/**
|
||||
* The batch mode option configures the formatting style for
|
||||
* multiple records. By default, multiple records will be
|
||||
* formatted as a JSON-encoded array. However, for
|
||||
* compatibility with some API endpoints, alternive styles
|
||||
* are available.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getBatchMode()
|
||||
{
|
||||
return $this->batch_mode;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
@@ -32,7 +60,46 @@ class JsonFormatter implements FormatterInterface
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function formatBatch(array $records)
|
||||
{
|
||||
switch ($this->batch_mode) {
|
||||
|
||||
case static::BATCH_MODE_NEWLINES:
|
||||
return $this->formatBatchNewlines($records);
|
||||
|
||||
case static::BATCH_MODE_JSON:
|
||||
default:
|
||||
return $this->formatBatchJson($records);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a JSON-encoded array of records.
|
||||
*
|
||||
* @param array $records
|
||||
* @return string
|
||||
*/
|
||||
protected function formatBatchJson(array $records)
|
||||
{
|
||||
return json_encode($records);
|
||||
}
|
||||
|
||||
/**
|
||||
* Use new lines to separate records instead of a
|
||||
* JSON-encoded array.
|
||||
*
|
||||
* @param array $records
|
||||
* @return string
|
||||
*/
|
||||
protected function formatBatchNewlines(array $records)
|
||||
{
|
||||
$instance = $this;
|
||||
|
||||
array_walk($records, function(&$value, $key) use ($instance) {
|
||||
$value = $instance->format($value);
|
||||
});
|
||||
|
||||
return implode("\n", $records);
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user