Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/config.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
blank_issues_enabled: false
blank_issues_enabled: true
contact_links:
- name: Ask a question
url: https://github.com/opcodesio/log-viewer/discussions/new?category=q-a
Expand Down
14 changes: 7 additions & 7 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
.phpunit.result.cache
.phpunit.cache
build
composer.lock
coverage
docs
logs
/composer.lock
/coverage
/docs
/logs
phpunit.xml
phpstan.neon
testbench.yaml
vendor
node_modules
tests/Feature/performance_test.log
/vendor
/node_modules
/tests/Feature/performance_test.log
8 changes: 6 additions & 2 deletions src/Logs/LaravelLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ protected function extractContextsFromFullText(): void
$json_data = json_decode(trim($json_string), true);

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

if (json_last_error() == JSON_ERROR_NONE) {
Expand Down Expand Up @@ -193,6 +193,10 @@ protected function getJsonStringsFromFullText(): array

protected function filterStackTrace(string $text): string
{
// Normalize line endings for cross-platform compatibility
$text = str_replace("\r\n", "\n", $text);
$text = str_replace("\r", "\n", $text);

$lines = explode("\n", $text);
$filteredLines = [];
$emptyLineCharacter = ' ...';
Expand Down
64 changes: 64 additions & 0 deletions tests/Unit/LaravelLogs/LaravelLogsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,3 +255,67 @@
'size_formatted' => Utils::bytesForHumans(strlen($messageString) - strlen('[2023-08-24 15:51:14] local.DEBUG: ')),
]);
});

it('filters stack traces in context when shorter stack traces is enabled', function () {
session(['log-viewer:shorter-stack-traces' => true]);
config([
'log-viewer.shorter_stack_trace_excludes' => [
'/vendor/symfony/',
'/vendor/laravel/framework/',
],
]);

$stackTrace = <<<'EOF'
#0 /app/Controllers/UserController.php(25): someFunction()
#1 /vendor/symfony/http-kernel/HttpKernel.php(158): handle()
#2 /vendor/laravel/framework/Illuminate/Pipeline/Pipeline.php(128): process()
#3 /app/Middleware/CustomMiddleware.php(42): handle()
#4 /vendor/symfony/routing/Router.php(89): route()
#5 /app/bootstrap/app.php(15): bootstrap()
EOF;

$logText = <<<EOF
[2024-10-18 12:00:00] production.ERROR: Exception occurred {"exception":"$stackTrace"}
EOF;

$log = new LaravelLog($logText);

expect($log->context)->toHaveKey('exception')
->and($log->context['exception'])->toContain('#0 /app/Controllers/UserController.php(25): someFunction()')
->and($log->context['exception'])->toContain('#3 /app/Middleware/CustomMiddleware.php(42): handle()')
->and($log->context['exception'])->toContain('#5 /app/bootstrap/app.php(15): bootstrap()')
->and($log->context['exception'])->toContain(' ...')
->and($log->context['exception'])->not->toContain('/vendor/symfony/http-kernel/')
->and($log->context['exception'])->not->toContain('/vendor/laravel/framework/')
->and($log->context['exception'])->not->toContain('/vendor/symfony/routing/');
});

it('does not filter context when shorter stack traces is disabled', function () {
session(['log-viewer:shorter-stack-traces' => false]);
config([
'log-viewer.shorter_stack_trace_excludes' => [
'/vendor/symfony/',
'/vendor/laravel/framework/',
],
]);

$stackTrace = <<<'EOF'
#0 /app/Controllers/UserController.php(25): someFunction()
#1 /vendor/symfony/http-kernel/HttpKernel.php(158): handle()
#2 /vendor/laravel/framework/Illuminate/Pipeline/Pipeline.php(128): process()
EOF;

$logText = <<<EOF
[2024-10-18 12:00:00] production.ERROR: Exception occurred {"exception":"$stackTrace"}
EOF;

$log = new LaravelLog($logText);

// Normalize expected value for cross-platform comparison (heredocs may have platform line endings)
$expectedStackTrace = str_replace(["\r\n", "\r"], "\n", $stackTrace);

expect($log->context)->toHaveKey('exception')
->and($log->context['exception'])->toBe($expectedStackTrace)
->and($log->context['exception'])->toContain('/vendor/symfony/http-kernel/')
->and($log->context['exception'])->toContain('/vendor/laravel/framework/');
});
Loading