Преглед на файлове

Merge pull request #345 from snapshotpl/formater

Fix #150: Table formatter for FirePHP
Jordi Boggiano преди 11 години
родител
ревизия
677ad16d6e
променени са 2 файла, в които са добавени 44 реда и са изтрити 2 реда
  1. 13 2
      src/Monolog/Formatter/WildfireFormatter.php
  2. 31 0
      tests/Monolog/Formatter/WildfireFormatterTest.php

+ 13 - 2
src/Monolog/Formatter/WildfireFormatter.php

@@ -22,6 +22,8 @@ use Monolog\Logger;
  */
 class WildfireFormatter extends NormalizerFormatter
 {
+    const TABLE = 'table';
+
     /**
      * Translates Monolog log levels to Wildfire levels.
      */
@@ -67,13 +69,22 @@ class WildfireFormatter extends NormalizerFormatter
             $message = reset($message);
         }
 
+        if(isset($record['context'][self::TABLE])){
+            $type  = 'TABLE';
+            $label = $record['channel'] .': '. $record['message'];
+            $message = $record['context'][self::TABLE];
+        } else {
+            $type  = $this->logLevels[$record['level']];
+            $label = $record['channel'];
+        }
+
         // Create JSON object describing the appearance of the message in the console
         $json = $this->toJson(array(
             array(
-                'Type'  => $this->logLevels[$record['level']],
+                'Type'  => $type,
                 'File'  => $file,
                 'Line'  => $line,
-                'Label' => $record['channel'],
+                'Label' => $label,
             ),
             $message,
         ), $handleError);

+ 31 - 0
tests/Monolog/Formatter/WildfireFormatterTest.php

@@ -108,4 +108,35 @@ class WildfireFormatterTest extends \PHPUnit_Framework_TestCase
 
         $wildfire->formatBatch(array($record));
     }
+
+    /**
+     * @covers Monolog\Formatter\WildfireFormatter::format
+     */
+    public function testTableFormat()
+    {
+        $wildfire = new WildfireFormatter();
+        $record = array(
+            'level' => Logger::ERROR,
+            'level_name' => 'ERROR',
+            'channel' => 'table-channel',
+            'context' => array(
+            WildfireFormatter::TABLE => array(
+                    array('col1', 'col2', 'col3'),
+                    array('val1', 'val2', 'val3'),
+                    array('foo1', 'foo2', 'foo3'),
+                    array('bar1', 'bar2', 'bar3'),
+                ),
+            ),
+            'datetime' => new \DateTime("@0"),
+            'extra' => array(),
+            'message' => 'table-message',
+        );
+
+        $message = $wildfire->format($record);
+
+        $this->assertEquals(
+            '171|[{"Type":"TABLE","File":"","Line":"","Label":"table-channel: table-message"},[["col1","col2","col3"],["val1","val2","val3"],["foo1","foo2","foo3"],["bar1","bar2","bar3"]]]|',
+            $message
+        );
+    }
 }