Explorar o código

Refactor scalar formatter

Removed some of its own logic in favor of extending NormalizerFormatter to
benefit from shared logic.
adlawson %!s(int64=12) %!d(string=hai) anos
pai
achega
ac091bffcd

+ 8 - 83
src/Monolog/Formatter/ScalarFormatter.php

@@ -11,7 +11,7 @@
 
 
 namespace Monolog\Formatter;
 namespace Monolog\Formatter;
 
 
-use Monolog\Formatter\FormatterInterface;
+use Monolog\Formatter\NormalizerFormatter;
 
 
 /**
 /**
  * Formats data into an associative array of scalar values.
  * Formats data into an associative array of scalar values.
@@ -19,107 +19,32 @@ use Monolog\Formatter\FormatterInterface;
  *
  *
  * @author Andrew Lawson <adlawson@gmail.com>
  * @author Andrew Lawson <adlawson@gmail.com>
  */
  */
-class ScalarFormatter implements FormatterInterface
+class ScalarFormatter extends NormalizerFormatter
 {
 {
-    /**
-     * @var string
-     */
-    protected $dateFormat;
-
-    /**
-     * @param string $dateFormat
-     */
-    public function __construct($dateFormat = \DateTime::ISO8601)
-    {
-        $this->dateFormat = $dateFormat;
-    }
-
     /**
     /**
      * {@inheritdoc}
      * {@inheritdoc}
      */
      */
     public function format(array $record)
     public function format(array $record)
     {
     {
-        $formatted = array();
-        $record = $this->exposeContext($record);
-
         foreach ($record as $key => $value) {
         foreach ($record as $key => $value) {
-            $formatted[$key] = $this->normalizeValue($value);
-        }
-
-        return $formatted;
-    }
-
-    /**
-     * {@inheritdoc}
-     */
-    public function formatBatch(array $records)
-    {
-        $formatted = array();
-
-        foreach ($records as $record) {
-            $formatted[] = $this->format($record);
-        }
-
-        return $formatted;
-    }
-
-    /**
-     * @param mixed $data
-     * @return string
-     */
-    protected function encodeData($data)
-    {
-        return json_encode($data, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT);
-    }
-
-    /**
-     * @param array $record
-     * @return array
-     */
-    protected function exposeContext(array $record)
-    {
-        if (isset($record['context'])) {
-            $context = $record['context'];
-
-            if (isset($context['exception'])) {
-                $record['context']['exception'] = $this->normalizeException($context['exception']);
-            }
+            $record[$key] = $this->normalizeValue($value);
         }
         }
 
 
         return $record;
         return $record;
     }
     }
 
 
-    /**
-     * @param Exception $e
-     * @return string
-     */
-    protected function normalizeException(\Exception $e)
-    {
-        return array(
-            'message' => $e->getMessage(),
-            'code'  => $e->getCode(),
-            'class' => get_class($e),
-            'file' => $e->getFile(),
-            'line' => $e->getLine(),
-            'trace' => $e->getTraceAsString(),
-            'debug' => $e->getTrace()
-        );
-    }
-
     /**
     /**
      * @param mixed $value
      * @param mixed $value
      * @return mixed
      * @return mixed
      */
      */
     protected function normalizeValue($value)
     protected function normalizeValue($value)
     {
     {
-        if ($value instanceof \DateTime) {
-            return $value->format($this->dateFormat);
-        } elseif ($value instanceof \Exception) {
-            return $this->encodeData($this->normalizeException($value));
-        } elseif (is_array($value) || is_object($value)) {
-            return $this->encodeData($value);
+        $normalized = $this->normalize($value);
+
+        if (is_array($normalized) || is_object($normalized)) {
+            return $this->toJson($normalized, true);
         }
         }
 
 
-        return $value;
+        return $normalized;
     }
     }
 }
 }

+ 28 - 14
tests/Monolog/Formatter/ScalarFormatterTest.php

@@ -8,9 +8,29 @@ class ScalarFormatterTest extends \PHPUnit_Framework_TestCase
         $this->formatter = new ScalarFormatter();
         $this->formatter = new ScalarFormatter();
     }
     }
 
 
+    public function buildTrace(\Exception $e)
+    {
+        $data = array();
+        $trace = $e->getTrace();
+        array_shift($trace);
+        foreach ($trace as $frame) {
+            if (isset($frame['file'])) {
+                $data[] = $frame['file'].':'.$frame['line'];
+            } else {
+                $data[] = json_encode($frame);
+            }
+        }
+
+        return $data;
+    }
+
     public function encodeJson($data)
     public function encodeJson($data)
     {
     {
-        return json_encode($data, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT);
+        if (version_compare(PHP_VERSION, '5.4.0', '>=')) {
+            return json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
+        }
+
+        return json_encode($data);
     }
     }
 
 
     public function testFormat()
     public function testFormat()
@@ -32,15 +52,12 @@ class ScalarFormatterTest extends \PHPUnit_Framework_TestCase
             'baz' => false,
             'baz' => false,
             'bam' => $this->encodeJson(array(1,2,3)),
             'bam' => $this->encodeJson(array(1,2,3)),
             'bat' => $this->encodeJson(array('foo' => 'bar')),
             'bat' => $this->encodeJson(array('foo' => 'bar')),
-            'bap' => '1970-01-01T00:00:00+0000',
+            'bap' => '1970-01-01 00:00:00',
             'ban' => $this->encodeJson(array(
             'ban' => $this->encodeJson(array(
-                'message' => $exception->getMessage(),
-                'code'    => $exception->getCode(),
                 'class'   => get_class($exception),
                 'class'   => get_class($exception),
-                'file'    => $exception->getFile(),
-                'line'    => $exception->getLine(),
-                'trace'   => $exception->getTraceAsString(),
-                'debug'   => $exception->getTrace()
+                'message' => $exception->getMessage(),
+                'file'    => $exception->getFile() . ':' . $exception->getLine(),
+                'trace'   => $this->buildTrace($exception)
             ))
             ))
         ), $formatted);
         ), $formatted);
     }
     }
@@ -69,13 +86,10 @@ class ScalarFormatterTest extends \PHPUnit_Framework_TestCase
         $this->assertSame(array(
         $this->assertSame(array(
             'context' => $this->encodeJson(array(
             'context' => $this->encodeJson(array(
                 'exception' => array(
                 'exception' => array(
-                    'message' => $exception->getMessage(),
-                    'code'    => $exception->getCode(),
                     'class'   => get_class($exception),
                     'class'   => get_class($exception),
-                    'file'    => $exception->getFile(),
-                    'line'    => $exception->getLine(),
-                    'trace'   => $exception->getTraceAsString(),
-                    'debug'   => $exception->getTrace()
+                    'message' => $exception->getMessage(),
+                    'file'    => $exception->getFile() . ':' . $exception->getLine(),
+                    'trace'   => $this->buildTrace($exception)
                 )
                 )
             ))
             ))
         ), $formatted);
         ), $formatted);