From 2aa09265fc1378072a0befec1e2891bc51369f1d Mon Sep 17 00:00:00 2001 From: Gunnar Lium Date: Fri, 14 Feb 2014 12:46:14 +0100 Subject: [PATCH] Strip inline line breaks from LineFormatter entries. --- src/Monolog/Formatter/LineFormatter.php | 20 +++++++++++-- tests/Monolog/Formatter/LineFormatterTest.php | 28 +++++++++++++++++++ 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/src/Monolog/Formatter/LineFormatter.php b/src/Monolog/Formatter/LineFormatter.php index af3d14b1..26f7aa12 100644 --- a/src/Monolog/Formatter/LineFormatter.php +++ b/src/Monolog/Formatter/LineFormatter.php @@ -26,14 +26,17 @@ class LineFormatter extends NormalizerFormatter const SIMPLE_FORMAT = "[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n"; 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->allowInlineLineBreaks = $allowInlineLineBreaks; parent::__construct($dateFormat); } @@ -57,6 +60,10 @@ class LineFormatter extends NormalizerFormatter } } + if (!$this->allowInlineLineBreaks) { + $output = $this->replaceInlineLineBreaks($output); + } + return $output; } @@ -98,4 +105,11 @@ class LineFormatter extends NormalizerFormatter return str_replace('\\/', '/', @json_encode($data)); } + + private function replaceInlineLineBreaks($output) + { + $suffix = substr($output, -1) === "\n" ? "\n" : ''; + + return trim(str_replace("\n", ' ', $output)) . $suffix; + } } diff --git a/tests/Monolog/Formatter/LineFormatterTest.php b/tests/Monolog/Formatter/LineFormatterTest.php index 7d7643a7..096e5644 100644 --- a/tests/Monolog/Formatter/LineFormatterTest.php +++ b/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); } + + 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