From df988599af32210955828a1f0275a5ae177dfb47 Mon Sep 17 00:00:00 2001 From: cesarreyes3 <109559100+cesarreyes3@users.noreply.github.com> Date: Wed, 27 Mar 2024 17:17:29 -0500 Subject: [PATCH 1/4] Remove base path on LineFormatter --- src/Monolog/Formatter/LineFormatter.php | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/Monolog/Formatter/LineFormatter.php b/src/Monolog/Formatter/LineFormatter.php index 025572a5c..c108183c7 100644 --- a/src/Monolog/Formatter/LineFormatter.php +++ b/src/Monolog/Formatter/LineFormatter.php @@ -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,17 @@ public function __construct(?string $format = null, ?string $dateFormat = null, parent::__construct($dateFormat); } + public function basePath(string $path = ''): self + { + if ($path !== '') { + $path = rtrim($path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR; + } + + $this->basePath = $path; + + return $this; + } + /** * @return $this */ @@ -258,7 +270,13 @@ private function formatException(\Throwable $e): string } } } - $str .= '): ' . $e->getMessage() . ' at ' . $e->getFile() . ':' . $e->getLine() . ')'; + + $file = $e->getFile(); + if ($this->basePath !== '') { + $file = str_replace($this->basePath, '', $e->getFile()); + } + + $str .= '): ' . $e->getMessage() . ' at ' . $file . ':' . $e->getLine() . ')'; if ($this->includeStacktraces) { $str .= $this->stacktracesParser($e); @@ -271,6 +289,10 @@ private function stacktracesParser(\Throwable $e): string { $trace = $e->getTraceAsString(); + if ($this->basePath !== '') { + $trace = str_replace(' ' . $this->basePath, ' ', $trace); + } + if ($this->stacktracesParser !== null) { $trace = $this->stacktracesParserCustom($trace); } From a37aa927bea6ecf5e8d322f1c377a742ec2cf428 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Fri, 12 Apr 2024 14:00:44 +0200 Subject: [PATCH 2/4] Apply suggestions from code review --- src/Monolog/Formatter/LineFormatter.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/Monolog/Formatter/LineFormatter.php b/src/Monolog/Formatter/LineFormatter.php index c108183c7..cf452450a 100644 --- a/src/Monolog/Formatter/LineFormatter.php +++ b/src/Monolog/Formatter/LineFormatter.php @@ -52,7 +52,11 @@ public function __construct(?string $format = null, ?string $dateFormat = null, parent::__construct($dateFormat); } - public function basePath(string $path = ''): self + /** + * 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; @@ -273,7 +277,7 @@ private function formatException(\Throwable $e): string $file = $e->getFile(); if ($this->basePath !== '') { - $file = str_replace($this->basePath, '', $e->getFile()); + $file = preg_replace('{^'.preg_quote($this->basePath).'}', '', $e->getFile()); } $str .= '): ' . $e->getMessage() . ' at ' . $file . ':' . $e->getLine() . ')'; @@ -290,7 +294,7 @@ private function stacktracesParser(\Throwable $e): string $trace = $e->getTraceAsString(); if ($this->basePath !== '') { - $trace = str_replace(' ' . $this->basePath, ' ', $trace); + $trace = preg_replace('{^(#\d+ )' . preg_quote($this->basePath) . '}m', '$1', $trace); } if ($this->stacktracesParser !== null) { From 4c10b642f46819be02158b5dff6b6742eb0d2a7f Mon Sep 17 00:00:00 2001 From: cesarreyes3 <109559100+cesarreyes3@users.noreply.github.com> Date: Fri, 12 Apr 2024 08:54:26 -0500 Subject: [PATCH 3/4] Fix phpstan test --- src/Monolog/Formatter/LineFormatter.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Monolog/Formatter/LineFormatter.php b/src/Monolog/Formatter/LineFormatter.php index cf452450a..a70754794 100644 --- a/src/Monolog/Formatter/LineFormatter.php +++ b/src/Monolog/Formatter/LineFormatter.php @@ -277,7 +277,7 @@ private function formatException(\Throwable $e): string $file = $e->getFile(); if ($this->basePath !== '') { - $file = preg_replace('{^'.preg_quote($this->basePath).'}', '', $e->getFile()); + $file = preg_replace('{^'.preg_quote($this->basePath).'}', '', $file); } $str .= '): ' . $e->getMessage() . ' at ' . $file . ':' . $e->getLine() . ')'; @@ -294,7 +294,7 @@ private function stacktracesParser(\Throwable $e): string $trace = $e->getTraceAsString(); if ($this->basePath !== '') { - $trace = preg_replace('{^(#\d+ )' . preg_quote($this->basePath) . '}m', '$1', $trace); + $trace = preg_replace('{^(#\d+ )' . preg_quote($this->basePath) . '}m', '$1', $trace) ?: $trace; } if ($this->stacktracesParser !== null) { From 6f5c5b96a4580fffb7a6afbddbbe121bc5f28cc7 Mon Sep 17 00:00:00 2001 From: cesarreyes3 <109559100+cesarreyes3@users.noreply.github.com> Date: Fri, 12 Apr 2024 10:59:20 -0500 Subject: [PATCH 4/4] Short ternary operator is not allowed, use null coalesce operator --- src/Monolog/Formatter/LineFormatter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Monolog/Formatter/LineFormatter.php b/src/Monolog/Formatter/LineFormatter.php index a70754794..7d35960cc 100644 --- a/src/Monolog/Formatter/LineFormatter.php +++ b/src/Monolog/Formatter/LineFormatter.php @@ -294,7 +294,7 @@ private function stacktracesParser(\Throwable $e): string $trace = $e->getTraceAsString(); if ($this->basePath !== '') { - $trace = preg_replace('{^(#\d+ )' . preg_quote($this->basePath) . '}m', '$1', $trace) ?: $trace; + $trace = preg_replace('{^(#\d+ )' . preg_quote($this->basePath) . '}m', '$1', $trace) ?? $trace; } if ($this->stacktracesParser !== null) {