From 623e2f317f1b154e3447da5651634c7b8109b467 Mon Sep 17 00:00:00 2001 From: Chris Carlson Date: Mon, 13 May 2024 13:17:23 -0600 Subject: [PATCH] Provide stack trace filtering in context --- src/Logs/LaravelLog.php | 48 +++++++++++++++++++++++++---------------- 1 file changed, 30 insertions(+), 18 deletions(-) diff --git a/src/Logs/LaravelLog.php b/src/Logs/LaravelLog.php index 475acab8..e6e48060 100644 --- a/src/Logs/LaravelLog.php +++ b/src/Logs/LaravelLog.php @@ -61,26 +61,13 @@ protected function parseText(array &$matches = []): void $text = $firstLineText.($matches[8] ?? '').implode('', $firstLineSplit)."\n".$theRestOfIt; if (session()->get('log-viewer:shorter-stack-traces', false)) { - $excludes = config('log-viewer.shorter_stack_trace_excludes', []); - $emptyLineCharacter = ' ...'; - $lines = explode("\n", $text); - $filteredLines = []; - foreach ($lines as $line) { - $shouldExclude = false; - foreach ($excludes as $excludePattern) { - if (str_starts_with($line, '#') && str_contains($line, $excludePattern)) { - $shouldExclude = true; - break; - } - } - - if ($shouldExclude && end($filteredLines) !== $emptyLineCharacter) { - $filteredLines[] = $emptyLineCharacter; - } elseif (! $shouldExclude) { - $filteredLines[] = $line; + // Filter stack traces in text and context. + $text = $this->filterStackTrace($text); + foreach ($this->context as $key => $value) { + if (is_string($value)) { + $this->context[$key] = $this->filterStackTrace($value); } } - $text = implode("\n", $filteredLines); } if (strlen($text) > LogViewer::maxLogSize()) { @@ -203,4 +190,29 @@ protected function getJsonStringsFromFullText(): array return $json_strings; } + + protected function filterStackTrace(string $text): string + { + $lines = explode("\n", $text); + $filteredLines = []; + $emptyLineCharacter = ' ...'; + $excludes = config('log-viewer.shorter_stack_trace_excludes', []); + foreach ($lines as $line) { + $shouldExclude = false; + foreach ($excludes as $excludePattern) { + if (str_starts_with($line, '#') && str_contains($line, $excludePattern)) { + $shouldExclude = true; + break; + } + } + + if ($shouldExclude && end($filteredLines) !== $emptyLineCharacter) { + $filteredLines[] = $emptyLineCharacter; + } elseif (! $shouldExclude) { + $filteredLines[] = $line; + } + } + + return implode("\n", $filteredLines); + } }