Skip to content

Commit 47e301d

Browse files
committed
Also add setBasePath to NormalizerFormatter/JsonFormatter
1 parent a4471eb commit 47e301d

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

src/Monolog/Formatter/NormalizerFormatter.php

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ class NormalizerFormatter implements FormatterInterface
3131

3232
private int $jsonEncodeOptions = Utils::DEFAULT_JSON_FLAGS;
3333

34+
protected string $basePath = '';
35+
3436
/**
3537
* @param string|null $dateFormat The format of the timestamp: one supported by DateTime::format
3638
* @throws \RuntimeException If the function json_encode does not exist
@@ -140,6 +142,21 @@ public function setJsonPrettyPrint(bool $enable): self
140142
return $this;
141143
}
142144

145+
/**
146+
* Setting a base path will hide the base path from exception and stack trace file names to shorten them
147+
* @return $this
148+
*/
149+
public function setBasePath(string $path = ''): self
150+
{
151+
if ($path !== '') {
152+
$path = rtrim($path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
153+
}
154+
155+
$this->basePath = $path;
156+
157+
return $this;
158+
}
159+
143160
/**
144161
* Provided as extension point
145162
*
@@ -247,11 +264,16 @@ protected function normalizeException(Throwable $e, int $depth = 0)
247264
return (array) $e->jsonSerialize();
248265
}
249266

267+
$file = $e->getFile();
268+
if ($this->basePath !== '') {
269+
$file = preg_replace('{^'.preg_quote($this->basePath).'}', '', $file);
270+
}
271+
250272
$data = [
251273
'class' => Utils::getClass($e),
252274
'message' => $e->getMessage(),
253275
'code' => (int) $e->getCode(),
254-
'file' => $e->getFile().':'.$e->getLine(),
276+
'file' => $file.':'.$e->getLine(),
255277
];
256278

257279
if ($e instanceof \SoapFault) {
@@ -275,7 +297,11 @@ protected function normalizeException(Throwable $e, int $depth = 0)
275297
$trace = $e->getTrace();
276298
foreach ($trace as $frame) {
277299
if (isset($frame['file'], $frame['line'])) {
278-
$data['trace'][] = $frame['file'].':'.$frame['line'];
300+
$file = $frame['file'];
301+
if ($this->basePath !== '') {
302+
$file = preg_replace('{^'.preg_quote($this->basePath).'}', '', $file);
303+
}
304+
$data['trace'][] = $file.':'.$frame['line'];
279305
}
280306
}
281307

tests/Monolog/Formatter/JsonFormatterTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,18 @@ public function testDefFormatWithException()
118118
$this->assertContextContainsFormattedException($formattedException, $message);
119119
}
120120

121+
public function testBasePathWithException(): void
122+
{
123+
$formatter = new JsonFormatter();
124+
$formatter->setBasePath(dirname(dirname(dirname(__DIR__))));
125+
$exception = new \RuntimeException('Foo');
126+
127+
$message = $this->formatRecordWithExceptionInContext($formatter, $exception);
128+
129+
$parsed = json_decode($message, true);
130+
self::assertSame('tests/Monolog/Formatter/JsonFormatterTest.php:' . (__LINE__ - 5), $parsed['context']['exception']['file']);
131+
}
132+
121133
public function testDefFormatWithPreviousException()
122134
{
123135
$formatter = new JsonFormatter();

tests/Monolog/Formatter/NormalizerFormatterTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,20 @@ public function testFormatExceptions()
8181
], $formatted);
8282
}
8383

84+
public function testFormatExceptionWithBasePath(): void
85+
{
86+
$formatter = new NormalizerFormatter('Y-m-d');
87+
$formatter->setBasePath(dirname(dirname(dirname(__DIR__))));
88+
$e = new \LogicException('bar');
89+
$formatted = $formatter->normalizeValue([
90+
'exception' => $e,
91+
]);
92+
93+
self::assertSame('tests/Monolog/Formatter/NormalizerFormatterTest.php:' . (__LINE__ - 5), $formatted['exception']['file']);
94+
self::assertStringStartsWith('vendor/phpunit/phpunit/src/Framework/TestCase.php:', $formatted['exception']['trace'][0]);
95+
self::assertStringStartsWith('vendor/phpunit/phpunit/src/Framework/TestCase.php:', $formatted['exception']['trace'][1]);
96+
}
97+
8498
public function testFormatSoapFaultException()
8599
{
86100
if (!class_exists('SoapFault')) {

0 commit comments

Comments
 (0)