1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-02-24 06:52:34 +01:00

Strip inline line breaks from LineFormatter entries.

This commit is contained in:
Gunnar Lium 2014-02-14 12:46:14 +01:00
parent 6cabe95f23
commit 2aa09265fc
2 changed files with 45 additions and 3 deletions

View File

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

View File

@ -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