Skip to content

Commit a576fdc

Browse files
authored
Merge pull request #373 from essell/enhancement/trace-filtering-in-context
Provide stack trace filtering in context
2 parents 8a6f6a3 + 623e2f3 commit a576fdc

File tree

1 file changed

+30
-18
lines changed

1 file changed

+30
-18
lines changed

src/Logs/LaravelLog.php

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -61,26 +61,13 @@ protected function parseText(array &$matches = []): void
6161
$text = $firstLineText.($matches[8] ?? '').implode('', $firstLineSplit)."\n".$theRestOfIt;
6262

6363
if (session()->get('log-viewer:shorter-stack-traces', false)) {
64-
$excludes = config('log-viewer.shorter_stack_trace_excludes', []);
65-
$emptyLineCharacter = ' ...';
66-
$lines = explode("\n", $text);
67-
$filteredLines = [];
68-
foreach ($lines as $line) {
69-
$shouldExclude = false;
70-
foreach ($excludes as $excludePattern) {
71-
if (str_starts_with($line, '#') && str_contains($line, $excludePattern)) {
72-
$shouldExclude = true;
73-
break;
74-
}
75-
}
76-
77-
if ($shouldExclude && end($filteredLines) !== $emptyLineCharacter) {
78-
$filteredLines[] = $emptyLineCharacter;
79-
} elseif (! $shouldExclude) {
80-
$filteredLines[] = $line;
64+
// Filter stack traces in text and context.
65+
$text = $this->filterStackTrace($text);
66+
foreach ($this->context as $key => $value) {
67+
if (is_string($value)) {
68+
$this->context[$key] = $this->filterStackTrace($value);
8169
}
8270
}
83-
$text = implode("\n", $filteredLines);
8471
}
8572

8673
if (strlen($text) > LogViewer::maxLogSize()) {
@@ -203,4 +190,29 @@ protected function getJsonStringsFromFullText(): array
203190

204191
return $json_strings;
205192
}
193+
194+
protected function filterStackTrace(string $text): string
195+
{
196+
$lines = explode("\n", $text);
197+
$filteredLines = [];
198+
$emptyLineCharacter = ' ...';
199+
$excludes = config('log-viewer.shorter_stack_trace_excludes', []);
200+
foreach ($lines as $line) {
201+
$shouldExclude = false;
202+
foreach ($excludes as $excludePattern) {
203+
if (str_starts_with($line, '#') && str_contains($line, $excludePattern)) {
204+
$shouldExclude = true;
205+
break;
206+
}
207+
}
208+
209+
if ($shouldExclude && end($filteredLines) !== $emptyLineCharacter) {
210+
$filteredLines[] = $emptyLineCharacter;
211+
} elseif (! $shouldExclude) {
212+
$filteredLines[] = $line;
213+
}
214+
}
215+
216+
return implode("\n", $filteredLines);
217+
}
206218
}

0 commit comments

Comments
 (0)