diff --git a/src/Monolog/Formatter/LineFormatter.php b/src/Monolog/Formatter/LineFormatter.php index 7cebc77d..025572a5 100644 --- a/src/Monolog/Formatter/LineFormatter.php +++ b/src/Monolog/Formatter/LineFormatter.php @@ -32,6 +32,7 @@ class LineFormatter extends NormalizerFormatter protected bool $ignoreEmptyContextAndExtra; protected bool $includeStacktraces; protected ?int $maxLevelNameLength = null; + protected string $indentStacktraces = ''; protected Closure|null $stacktracesParser = null; /** @@ -64,6 +65,19 @@ class LineFormatter extends NormalizerFormatter return $this; } + /** + * Indent stack traces to separate them a bit from the main log record messages + * + * @param string $indent The string used to indent, for example " " + * @return $this + */ + public function indentStacktraces(string $indent): self + { + $this->indentStacktraces = $indent; + + return $this; + } + /** * @return $this */ @@ -261,7 +275,11 @@ class LineFormatter extends NormalizerFormatter $trace = $this->stacktracesParserCustom($trace); } - return "\n[stacktrace]\n" . $trace . "\n"; + if ($this->indentStacktraces !== '') { + $trace = str_replace("\n", "\n{$this->indentStacktraces}", $trace); + } + + return "\n{$this->indentStacktraces}[stacktrace]\n{$this->indentStacktraces}" . $trace . "\n"; } private function stacktracesParserCustom(string $trace): string diff --git a/tests/Monolog/Formatter/LineFormatterTest.php b/tests/Monolog/Formatter/LineFormatterTest.php index 8c65e44c..e82c55dc 100644 --- a/tests/Monolog/Formatter/LineFormatterTest.php +++ b/tests/Monolog/Formatter/LineFormatterTest.php @@ -13,6 +13,7 @@ namespace Monolog\Formatter; use Monolog\Test\TestCase; use Monolog\Level; +use RuntimeException; /** * @covers Monolog\Formatter\LineFormatter @@ -277,6 +278,19 @@ class LineFormatterTest extends TestCase $this->assertMatchesRegularExpression('/foo\nbar/', $message); } + public function testIndentStackTraces(): void + { + $formatter = new LineFormatter(); + $formatter->includeStacktraces(); + //$formatter->allowInlineLineBreaks(); + $formatter->indentStackTraces(' '); + $message = $formatter->format($this->getRecord(message: "foo", context: ['exception' => new RuntimeException('lala')])); + + $this->assertStringContainsString(' [stacktrace]', $message); + $this->assertStringContainsString(' #0', $message); + $this->assertStringContainsString(' #1', $message); + } + /** * @dataProvider providerMaxLevelNameLength */