Skip to content

Commit d244fb6

Browse files
authored
Merge pull request #482 from opcodesio/test/context-stack-trace-filtering
Add tests for stack trace filtering in context
2 parents a576fdc + ddfeeb1 commit d244fb6

File tree

4 files changed

+78
-10
lines changed

4 files changed

+78
-10
lines changed

.github/ISSUE_TEMPLATE/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
blank_issues_enabled: false
1+
blank_issues_enabled: true
22
contact_links:
33
- name: Ask a question
44
url: https://github.com/opcodesio/log-viewer/discussions/new?category=q-a

.gitignore

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
.phpunit.result.cache
44
.phpunit.cache
55
build
6-
composer.lock
7-
coverage
8-
docs
9-
logs
6+
/composer.lock
7+
/coverage
8+
/docs
9+
/logs
1010
phpunit.xml
1111
phpstan.neon
1212
testbench.yaml
13-
vendor
14-
node_modules
15-
tests/Feature/performance_test.log
13+
/vendor
14+
/node_modules
15+
/tests/Feature/performance_test.log

src/Logs/LaravelLog.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,8 @@ protected function extractContextsFromFullText(): void
108108
$json_data = json_decode(trim($json_string), true);
109109

110110
if (json_last_error() == JSON_ERROR_CTRL_CHAR) {
111-
// might need to escape new lines
112-
$json_data = json_decode(str_replace("\n", '\\n', $json_string), true);
111+
// might need to escape new lines and carriage returns
112+
$json_data = json_decode(str_replace(["\r\n", "\r", "\n"], ['\\n', '\\n', '\\n'], $json_string), true);
113113
}
114114

115115
if (json_last_error() == JSON_ERROR_NONE) {
@@ -193,6 +193,10 @@ protected function getJsonStringsFromFullText(): array
193193

194194
protected function filterStackTrace(string $text): string
195195
{
196+
// Normalize line endings for cross-platform compatibility
197+
$text = str_replace("\r\n", "\n", $text);
198+
$text = str_replace("\r", "\n", $text);
199+
196200
$lines = explode("\n", $text);
197201
$filteredLines = [];
198202
$emptyLineCharacter = ' ...';

tests/Unit/LaravelLogs/LaravelLogsTest.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,3 +255,67 @@
255255
'size_formatted' => Utils::bytesForHumans(strlen($messageString) - strlen('[2023-08-24 15:51:14] local.DEBUG: ')),
256256
]);
257257
});
258+
259+
it('filters stack traces in context when shorter stack traces is enabled', function () {
260+
session(['log-viewer:shorter-stack-traces' => true]);
261+
config([
262+
'log-viewer.shorter_stack_trace_excludes' => [
263+
'/vendor/symfony/',
264+
'/vendor/laravel/framework/',
265+
],
266+
]);
267+
268+
$stackTrace = <<<'EOF'
269+
#0 /app/Controllers/UserController.php(25): someFunction()
270+
#1 /vendor/symfony/http-kernel/HttpKernel.php(158): handle()
271+
#2 /vendor/laravel/framework/Illuminate/Pipeline/Pipeline.php(128): process()
272+
#3 /app/Middleware/CustomMiddleware.php(42): handle()
273+
#4 /vendor/symfony/routing/Router.php(89): route()
274+
#5 /app/bootstrap/app.php(15): bootstrap()
275+
EOF;
276+
277+
$logText = <<<EOF
278+
[2024-10-18 12:00:00] production.ERROR: Exception occurred {"exception":"$stackTrace"}
279+
EOF;
280+
281+
$log = new LaravelLog($logText);
282+
283+
expect($log->context)->toHaveKey('exception')
284+
->and($log->context['exception'])->toContain('#0 /app/Controllers/UserController.php(25): someFunction()')
285+
->and($log->context['exception'])->toContain('#3 /app/Middleware/CustomMiddleware.php(42): handle()')
286+
->and($log->context['exception'])->toContain('#5 /app/bootstrap/app.php(15): bootstrap()')
287+
->and($log->context['exception'])->toContain(' ...')
288+
->and($log->context['exception'])->not->toContain('/vendor/symfony/http-kernel/')
289+
->and($log->context['exception'])->not->toContain('/vendor/laravel/framework/')
290+
->and($log->context['exception'])->not->toContain('/vendor/symfony/routing/');
291+
});
292+
293+
it('does not filter context when shorter stack traces is disabled', function () {
294+
session(['log-viewer:shorter-stack-traces' => false]);
295+
config([
296+
'log-viewer.shorter_stack_trace_excludes' => [
297+
'/vendor/symfony/',
298+
'/vendor/laravel/framework/',
299+
],
300+
]);
301+
302+
$stackTrace = <<<'EOF'
303+
#0 /app/Controllers/UserController.php(25): someFunction()
304+
#1 /vendor/symfony/http-kernel/HttpKernel.php(158): handle()
305+
#2 /vendor/laravel/framework/Illuminate/Pipeline/Pipeline.php(128): process()
306+
EOF;
307+
308+
$logText = <<<EOF
309+
[2024-10-18 12:00:00] production.ERROR: Exception occurred {"exception":"$stackTrace"}
310+
EOF;
311+
312+
$log = new LaravelLog($logText);
313+
314+
// Normalize expected value for cross-platform comparison (heredocs may have platform line endings)
315+
$expectedStackTrace = str_replace(["\r\n", "\r"], "\n", $stackTrace);
316+
317+
expect($log->context)->toHaveKey('exception')
318+
->and($log->context['exception'])->toBe($expectedStackTrace)
319+
->and($log->context['exception'])->toContain('/vendor/symfony/http-kernel/')
320+
->and($log->context['exception'])->toContain('/vendor/laravel/framework/');
321+
});

0 commit comments

Comments
 (0)