Переглянути джерело

Replaced LogglyFormatter with option to change batch formatting in JsonFormatter

Adam Pancutt 12 роки тому
батько
коміт
95f0649b59

+ 67 - 0
src/Monolog/Formatter/JsonFormatter.php

@@ -20,6 +20,34 @@ namespace Monolog\Formatter;
  */
  */
 class JsonFormatter implements FormatterInterface
 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}
      * {@inheritdoc}
      */
      */
@@ -32,7 +60,46 @@ class JsonFormatter implements FormatterInterface
      * {@inheritdoc}
      * {@inheritdoc}
      */
      */
     public function formatBatch(array $records)
     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);
         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);
+    }
+
 }
 }

+ 0 - 36
src/Monolog/Formatter/LogglyFormatter.php

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

+ 2 - 2
src/Monolog/Handler/LogglyHandler.php

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

+ 0 - 36
tests/Monolog/Formatter/LogglyFormatterTest.php

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