Skip to content

Commit

Permalink
Fix replaceNewlines method to avoid replacing escaped backslashes, cl…
Browse files Browse the repository at this point in the history
…oses #1721, fixes #1720
  • Loading branch information
abordage authored and Seldaek committed Jul 22, 2022
1 parent cf0f4b3 commit 320909a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/Monolog/Formatter/LineFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,11 @@ protected function replaceNewlines(string $str): string
{
if ($this->allowInlineLineBreaks) {
if (0 === strpos($str, '{')) {
return str_replace(array('\r', '\n'), array("\r", "\n"), $str);
$str = preg_replace('/(?<!\\\\)\\\\[rn]/', "\n", $str);
if (null === $str) {
$pcreErrorCode = preg_last_error();
throw new \RuntimeException('Failed to run preg_replace: ' . $pcreErrorCode . ' / ' . Utils::pcreLastErrorMessage($pcreErrorCode));
}
}

return $str;
Expand Down
8 changes: 8 additions & 0 deletions tests/Monolog/Formatter/LineFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,14 @@ public function testDefFormatWithExceptionAndStacktrace()
$this->assertRegexp('{^\['.date('Y-m-d').'] core\.CRITICAL: foobar \{"exception":"\[object] \(RuntimeException\(code: 0\): Foo at '.preg_quote(substr($path, 1, -1)).':'.(__LINE__ - 8).'\)\n\[stacktrace]\n#0}', $message);
}

public function testInlineLineBreaksRespectsEscapedBackslashes()
{
$formatter = new LineFormatter(null, 'Y-m-d');
$formatter->allowInlineLineBreaks();

self::assertSame('{"test":"foo'."\n".'bar\\\\name-with-n"}', $formatter->stringify(["test" => "foo\nbar\\name-with-n"]));
}

public function testDefFormatWithExceptionAndStacktraceParserFull()
{
$formatter = new LineFormatter(null, 'Y-m-d');
Expand Down

0 comments on commit 320909a

Please sign in to comment.