summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJordi Boggiano <j.boggiano@seld.be>2016-09-25 18:22:52 +0200
committerJordi Boggiano <j.boggiano@seld.be>2016-09-25 18:22:52 +0200
commitbb61bfbe3b7ec6fb9347f2b12e987a5b0094998d (patch)
tree4a4ed8795224c6648f2bb3ba51306c9f60ccd2b7
parent96f4fd718f8a547f5c5eeadf06383f097f4e3c08 (diff)
downloadmonolog-bb61bfbe3b7ec6fb9347f2b12e987a5b0094998d.zip
monolog-bb61bfbe3b7ec6fb9347f2b12e987a5b0094998d.tar.gz
monolog-bb61bfbe3b7ec6fb9347f2b12e987a5b0094998d.tar.bz2
When newlines are allowed in LineFormatter, unpack json_encoded blobs containing newlines into multiline strings, fixes #752
-rw-r--r--src/Monolog/Formatter/LineFormatter.php6
-rw-r--r--tests/Monolog/Formatter/LineFormatterTest.php18
2 files changed, 23 insertions, 1 deletions
diff --git a/src/Monolog/Formatter/LineFormatter.php b/src/Monolog/Formatter/LineFormatter.php
index 5a65b4c..3cce7f7 100644
--- a/src/Monolog/Formatter/LineFormatter.php
+++ b/src/Monolog/Formatter/LineFormatter.php
@@ -130,7 +130,7 @@ class LineFormatter extends NormalizerFormatter
$str = '[object] ('.get_class($e).'(code: '.$e->getCode().'): '.$e->getMessage().' at '.$e->getFile().':'.$e->getLine().$previousText.')';
if ($this->includeStacktraces) {
- $str .= "\n[stacktrace]\n".$e->getTraceAsString();
+ $str .= "\n[stacktrace]\n".$e->getTraceAsString()."\n";
}
return $str;
@@ -152,6 +152,10 @@ class LineFormatter extends NormalizerFormatter
protected function replaceNewlines($str)
{
if ($this->allowInlineLineBreaks) {
+ if (0 === strpos($str, '{')) {
+ return str_replace(['\r', '\n'], ["\r", "\n"], $str);
+ }
+
return $str;
}
diff --git a/tests/Monolog/Formatter/LineFormatterTest.php b/tests/Monolog/Formatter/LineFormatterTest.php
index cbbbc66..4fdcd8c 100644
--- a/tests/Monolog/Formatter/LineFormatterTest.php
+++ b/tests/Monolog/Formatter/LineFormatterTest.php
@@ -137,6 +137,24 @@ class LineFormatterTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('['.date('Y-m-d').'] core.CRITICAL: foobar {"exception":"[object] (RuntimeException(code: 0): Foo at '.substr($path, 1, -1).':'.(__LINE__ - 8).')"} []'."\n", $message);
}
+ public function testDefFormatWithExceptionAndStacktrace()
+ {
+ $formatter = new LineFormatter(null, 'Y-m-d');
+ $formatter->includeStacktraces();
+ $message = $formatter->format([
+ 'level_name' => 'CRITICAL',
+ 'channel' => 'core',
+ 'context' => ['exception' => new \RuntimeException('Foo')],
+ 'datetime' => new \DateTimeImmutable,
+ 'extra' => [],
+ 'message' => 'foobar',
+ ]);
+
+ $path = str_replace('\\/', '/', json_encode(__FILE__));
+
+ $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 testDefFormatWithPreviousException()
{
$formatter = new LineFormatter(null, 'Y-m-d');