1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-08-08 22:26:41 +02:00

Remove base path on LineFormatter (#1873)

This commit is contained in:
cesarreyes3
2024-04-12 15:37:56 -05:00
committed by GitHub
parent c48c642b9a
commit 24e0e454d9

View File

@@ -34,6 +34,7 @@ class LineFormatter extends NormalizerFormatter
protected ?int $maxLevelNameLength = null;
protected string $indentStacktraces = '';
protected Closure|null $stacktracesParser = null;
protected string $basePath = '';
/**
* @param string|null $format The format of the message
@@ -51,6 +52,21 @@ class LineFormatter extends NormalizerFormatter
parent::__construct($dateFormat);
}
/**
* 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;
}
/**
* @return $this
*/
@@ -258,7 +274,13 @@ class LineFormatter extends NormalizerFormatter
}
}
}
$str .= '): ' . $e->getMessage() . ' at ' . $e->getFile() . ':' . $e->getLine() . ')';
$file = $e->getFile();
if ($this->basePath !== '') {
$file = preg_replace('{^'.preg_quote($this->basePath).'}', '', $file);
}
$str .= '): ' . $e->getMessage() . ' at ' . $file . ':' . $e->getLine() . ')';
if ($this->includeStacktraces) {
$str .= $this->stacktracesParser($e);
@@ -271,6 +293,10 @@ class LineFormatter extends NormalizerFormatter
{
$trace = $e->getTraceAsString();
if ($this->basePath !== '') {
$trace = preg_replace('{^(#\d+ )' . preg_quote($this->basePath) . '}m', '$1', $trace) ?? $trace;
}
if ($this->stacktracesParser !== null) {
$trace = $this->stacktracesParserCustom($trace);
}