1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-07-31 10:20:14 +02:00

Replaced LogglyFormatter with option to change batch formatting in JsonFormatter

This commit is contained in:
Adam Pancutt
2014-01-21 11:39:58 +00:00
parent e17aad1a99
commit 95f0649b59
4 changed files with 69 additions and 74 deletions

View File

@@ -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);
}
}

View File

@@ -1,36 +0,0 @@
<?php
/*
* This file is part of the Monolog package.
*
* (c) Jordi Boggiano <j.boggiano@seld.be>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Monolog\Formatter;
/**
* Extends the standard JsonFormatter to format messages compatible with Loggly.
*
* @author Adam Pancutt <adam@pancutt.com>
*/
class LogglyFormatter extends JsonFormatter
{
/**
* {@inheritdoc}
*/
public function formatBatch(array $records)
{
$instance = $this;
array_walk($records, function(&$value, $key) use ($instance) {
$value = $instance->format($value);
});
return implode("\n", $records);
}
}

View File

@@ -12,7 +12,7 @@
namespace Monolog\Handler;
use Monolog\Logger;
use Monolog\Formatter\LogglyFormatter;
use Monolog\Formatter\JsonFormatter;
/**
* Sends errors to Loggly.
@@ -88,6 +88,6 @@ class LogglyHandler extends AbstractProcessingHandler
protected function getDefaultFormatter()
{
return new LogglyFormatter();
return new JsonFormatter(JsonFormatter::BATCH_MODE_NEWLINES);
}
}

View File

@@ -1,36 +0,0 @@
<?php
/*
* This file is part of the Monolog package.
*
* (c) Jordi Boggiano <j.boggiano@seld.be>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Monolog\Formatter;
use Monolog\Logger;
use Monolog\TestCase;
class LogglyFormatterTest extends TestCase
{
/**
* @covers Monolog\Formatter\LogglyFormatter::formatBatch
*/
public function testFormatBatch()
{
$formatter = new LogglyFormatter();
$records = $expected = array(
$this->getRecord(Logger::WARNING),
$this->getRecord(Logger::DEBUG),
);
array_walk($expected, function(&$value, $key) {
$value = json_encode($value);
});
$this->assertEquals(implode("\n", $expected), $formatter->formatBatch($records));
}
}