From 320909a1d1c68f134603469f866a6991c753639f Mon Sep 17 00:00:00 2001 From: Pavel Bychko Date: Sun, 26 Jun 2022 12:34:03 +0300 Subject: [PATCH] Fix replaceNewlines method to avoid replacing escaped backslashes, closes #1721, fixes #1720 --- src/Monolog/Formatter/LineFormatter.php | 6 +++++- tests/Monolog/Formatter/LineFormatterTest.php | 8 ++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/Monolog/Formatter/LineFormatter.php b/src/Monolog/Formatter/LineFormatter.php index 69dfee5ce..b31b2971a 100644 --- a/src/Monolog/Formatter/LineFormatter.php +++ b/src/Monolog/Formatter/LineFormatter.php @@ -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('/(?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');