Bläddra i källkod

Strip inline line breaks from LineFormatter entries.

Gunnar Lium 12 år sedan
förälder
incheckning
2aa09265fc

+ 17 - 3
src/Monolog/Formatter/LineFormatter.php

@@ -26,14 +26,17 @@ class LineFormatter extends NormalizerFormatter
     const SIMPLE_FORMAT = "[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n";
     const SIMPLE_FORMAT = "[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n";
 
 
     protected $format;
     protected $format;
+    protected $allowInlineLineBreaks;
 
 
     /**
     /**
-     * @param string $format     The format of the message
-     * @param string $dateFormat The format of the timestamp: one supported by DateTime::format
+     * @param string $format                The format of the message
+     * @param string $dateFormat            The format of the timestamp: one supported by DateTime::format
+     * @param bool   $allowInlineLineBreaks Whether to allow inline line breaks in log entries
      */
      */
-    public function __construct($format = null, $dateFormat = null)
+    public function __construct($format = null, $dateFormat = null, $allowInlineLineBreaks = false)
     {
     {
         $this->format = $format ?: static::SIMPLE_FORMAT;
         $this->format = $format ?: static::SIMPLE_FORMAT;
+        $this->allowInlineLineBreaks = $allowInlineLineBreaks;
         parent::__construct($dateFormat);
         parent::__construct($dateFormat);
     }
     }
 
 
@@ -57,6 +60,10 @@ class LineFormatter extends NormalizerFormatter
             }
             }
         }
         }
 
 
+        if (!$this->allowInlineLineBreaks) {
+            $output = $this->replaceInlineLineBreaks($output);
+        }
+
         return $output;
         return $output;
     }
     }
 
 
@@ -98,4 +105,11 @@ class LineFormatter extends NormalizerFormatter
 
 
         return str_replace('\\/', '/', @json_encode($data));
         return str_replace('\\/', '/', @json_encode($data));
     }
     }
+
+    private function replaceInlineLineBreaks($output)
+    {
+        $suffix = substr($output, -1) === "\n" ? "\n" : '';
+
+        return trim(str_replace("\n", ' ', $output)) . $suffix;
+    }
 }
 }

+ 28 - 0
tests/Monolog/Formatter/LineFormatterTest.php

@@ -150,6 +150,34 @@ class LineFormatterTest extends \PHPUnit_Framework_TestCase
         ));
         ));
         $this->assertEquals('['.date('Y-m-d').'] test.CRITICAL: bar [] []'."\n".'['.date('Y-m-d').'] log.WARNING: foo [] []'."\n", $message);
         $this->assertEquals('['.date('Y-m-d').'] test.CRITICAL: bar [] []'."\n".'['.date('Y-m-d').'] log.WARNING: foo [] []'."\n", $message);
     }
     }
+
+    public function testFormatShouldStripInlineLineBreaks()
+    {
+        $formatter = new LineFormatter(null, 'Y-m-d');
+        $message = $formatter->format(
+            array(
+                'message' => "foo\nbar",
+                'context' => array(),
+                'extra' => array(),
+            )
+        );
+
+        $this->assertRegExp('/foo bar/', $message);
+    }
+
+    public function testFormatShouldNotStripInlineLineBreaksWhenFlagIsSet()
+    {
+        $formatter = new LineFormatter(null, 'Y-m-d', true);
+        $message = $formatter->format(
+            array(
+                'message' => "foo\nbar",
+                'context' => array(),
+                'extra' => array(),
+            )
+        );
+
+        $this->assertRegExp('/foo\nbar/', $message);
+    }
 }
 }
 
 
 class TestFoo
 class TestFoo