From 95f0649b5931bc8423319dad2517947005dcec05 Mon Sep 17 00:00:00 2001 From: Adam Pancutt Date: Tue, 21 Jan 2014 11:39:58 +0000 Subject: [PATCH] Replaced LogglyFormatter with option to change batch formatting in JsonFormatter --- src/Monolog/Formatter/JsonFormatter.php | 67 +++++++++++++++++++ src/Monolog/Formatter/LogglyFormatter.php | 36 ---------- src/Monolog/Handler/LogglyHandler.php | 4 +- .../Monolog/Formatter/LogglyFormatterTest.php | 36 ---------- 4 files changed, 69 insertions(+), 74 deletions(-) delete mode 100644 src/Monolog/Formatter/LogglyFormatter.php delete mode 100644 tests/Monolog/Formatter/LogglyFormatterTest.php diff --git a/src/Monolog/Formatter/JsonFormatter.php b/src/Monolog/Formatter/JsonFormatter.php index 822af0ea..62c70026 100644 --- a/src/Monolog/Formatter/JsonFormatter.php +++ b/src/Monolog/Formatter/JsonFormatter.php @@ -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); + } + } diff --git a/src/Monolog/Formatter/LogglyFormatter.php b/src/Monolog/Formatter/LogglyFormatter.php deleted file mode 100644 index 46062aa9..00000000 --- a/src/Monolog/Formatter/LogglyFormatter.php +++ /dev/null @@ -1,36 +0,0 @@ - - * - * 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 - */ -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); - } - -} diff --git a/src/Monolog/Handler/LogglyHandler.php b/src/Monolog/Handler/LogglyHandler.php index 9a3de6e6..ba2c7f11 100644 --- a/src/Monolog/Handler/LogglyHandler.php +++ b/src/Monolog/Handler/LogglyHandler.php @@ -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); } } diff --git a/tests/Monolog/Formatter/LogglyFormatterTest.php b/tests/Monolog/Formatter/LogglyFormatterTest.php deleted file mode 100644 index a6d8d57a..00000000 --- a/tests/Monolog/Formatter/LogglyFormatterTest.php +++ /dev/null @@ -1,36 +0,0 @@ - - * - * 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)); - } - -}