Skip to content

Commit

Permalink
Also add setBasePath to NormalizerFormatter/JsonFormatter
Browse files Browse the repository at this point in the history
  • Loading branch information
Seldaek committed Jun 28, 2024
1 parent a4471eb commit 47e301d
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
30 changes: 28 additions & 2 deletions src/Monolog/Formatter/NormalizerFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class NormalizerFormatter implements FormatterInterface

private int $jsonEncodeOptions = Utils::DEFAULT_JSON_FLAGS;

protected string $basePath = '';

/**
* @param string|null $dateFormat The format of the timestamp: one supported by DateTime::format
* @throws \RuntimeException If the function json_encode does not exist
Expand Down Expand Up @@ -140,6 +142,21 @@ public function setJsonPrettyPrint(bool $enable): self
return $this;
}

/**
* Setting a base path will hide the base path from exception and stack trace file names to shorten them
* @return $this
*/
public function setBasePath(string $path = ''): self
{
if ($path !== '') {
$path = rtrim($path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
}

$this->basePath = $path;

return $this;
}

/**
* Provided as extension point
*
Expand Down Expand Up @@ -247,11 +264,16 @@ protected function normalizeException(Throwable $e, int $depth = 0)
return (array) $e->jsonSerialize();
}

$file = $e->getFile();
if ($this->basePath !== '') {
$file = preg_replace('{^'.preg_quote($this->basePath).'}', '', $file);
}

$data = [
'class' => Utils::getClass($e),
'message' => $e->getMessage(),
'code' => (int) $e->getCode(),
'file' => $e->getFile().':'.$e->getLine(),
'file' => $file.':'.$e->getLine(),
];

if ($e instanceof \SoapFault) {
Expand All @@ -275,7 +297,11 @@ protected function normalizeException(Throwable $e, int $depth = 0)
$trace = $e->getTrace();
foreach ($trace as $frame) {
if (isset($frame['file'], $frame['line'])) {
$data['trace'][] = $frame['file'].':'.$frame['line'];
$file = $frame['file'];
if ($this->basePath !== '') {
$file = preg_replace('{^'.preg_quote($this->basePath).'}', '', $file);
}
$data['trace'][] = $file.':'.$frame['line'];
}
}

Expand Down
12 changes: 12 additions & 0 deletions tests/Monolog/Formatter/JsonFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,18 @@ public function testDefFormatWithException()
$this->assertContextContainsFormattedException($formattedException, $message);
}

public function testBasePathWithException(): void
{
$formatter = new JsonFormatter();
$formatter->setBasePath(dirname(dirname(dirname(__DIR__))));
$exception = new \RuntimeException('Foo');

$message = $this->formatRecordWithExceptionInContext($formatter, $exception);

$parsed = json_decode($message, true);
self::assertSame('tests/Monolog/Formatter/JsonFormatterTest.php:' . (__LINE__ - 5), $parsed['context']['exception']['file']);
}

public function testDefFormatWithPreviousException()
{
$formatter = new JsonFormatter();
Expand Down
14 changes: 14 additions & 0 deletions tests/Monolog/Formatter/NormalizerFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,20 @@ public function testFormatExceptions()
], $formatted);
}

public function testFormatExceptionWithBasePath(): void
{
$formatter = new NormalizerFormatter('Y-m-d');
$formatter->setBasePath(dirname(dirname(dirname(__DIR__))));
$e = new \LogicException('bar');
$formatted = $formatter->normalizeValue([
'exception' => $e,
]);

self::assertSame('tests/Monolog/Formatter/NormalizerFormatterTest.php:' . (__LINE__ - 5), $formatted['exception']['file']);
self::assertStringStartsWith('vendor/phpunit/phpunit/src/Framework/TestCase.php:', $formatted['exception']['trace'][0]);
self::assertStringStartsWith('vendor/phpunit/phpunit/src/Framework/TestCase.php:', $formatted['exception']['trace'][1]);
}

public function testFormatSoapFaultException()
{
if (!class_exists('SoapFault')) {
Expand Down

0 comments on commit 47e301d

Please sign in to comment.