diff --git a/config/log-viewer.php b/config/log-viewer.php index d475cd3a..1ef10612 100644 --- a/config/log-viewer.php +++ b/config/log-viewer.php @@ -292,6 +292,19 @@ ], + /* + |-------------------------------------------------------------------------- + | Exclude IP from identifiers + |-------------------------------------------------------------------------- + | By default, file and folder identifiers include the server's IP address + | to ensure uniqueness. In load-balanced environments with shared storage, + | this can cause "No results" errors. Set to true to exclude IP addresses + | from identifier generation for consistent results across servers. + | + */ + + 'exclude_ip_from_identifiers' => env('LOG_VIEWER_EXCLUDE_IP_FROM_IDENTIFIERS', false), + /* |-------------------------------------------------------------------------- | Root folder prefix diff --git a/src/LogFile.php b/src/LogFile.php index eb6d9bb3..51e1ae6a 100644 --- a/src/LogFile.php +++ b/src/LogFile.php @@ -29,7 +29,13 @@ public function __construct(string $path, ?string $type = null, ?string $pathAli { $this->path = $path; $this->name = basename($path); - $this->identifier = Utils::shortMd5(Utils::getLocalIP().':'.$path).'-'.$this->name; + + if (config('log-viewer.exclude_ip_from_identifiers', false)) { + $this->identifier = Utils::shortMd5($path).'-'.$this->name; + } else { + $this->identifier = Utils::shortMd5(Utils::getLocalIP().':'.$path).'-'.$this->name; + } + $this->type = $type; $this->displayPath = empty($pathAlias) ? $path @@ -102,7 +108,11 @@ public function sizeFormatted(): string public function subFolderIdentifier(): string { - return Utils::shortMd5(Utils::getLocalIP().':'.$this->subFolder); + if (config('log-viewer.exclude_ip_from_identifiers', false)) { + return Utils::shortMd5($this->subFolder); + } else { + return Utils::shortMd5(Utils::getLocalIP().':'.$this->subFolder); + } } public function downloadUrl(): string diff --git a/src/LogFolder.php b/src/LogFolder.php index 40f0f2d0..110cbdf0 100644 --- a/src/LogFolder.php +++ b/src/LogFolder.php @@ -17,7 +17,12 @@ public function __construct( public string $path, mixed $files, ) { - $this->identifier = Utils::shortMd5(Utils::getLocalIP().':'.$path); + if (config('log-viewer.exclude_ip_from_identifiers', false)) { + $this->identifier = Utils::shortMd5($path); + } else { + $this->identifier = Utils::shortMd5(Utils::getLocalIP().':'.$path); + } + $this->files = new LogFileCollection($files); } diff --git a/tests/Unit/LogFileTest.php b/tests/Unit/LogFileTest.php index 3e57aaca..5a3506f3 100644 --- a/tests/Unit/LogFileTest.php +++ b/tests/Unit/LogFileTest.php @@ -39,3 +39,24 @@ Utils::shortMd5($serverIp.':'.$logFile->subFolder) ); }); + +test('log file identifier excludes IP when config is enabled', function () { + $path = storage_path('logs/laravel.log'); + file_put_contents($path, str_repeat('0', 10)); // 10 bytes + // Set the cached local IP to a known value: + Utils::setCachedLocalIP($serverIp = '123.123.123.123'); + + // Enable the config to exclude IP from identifiers + config(['log-viewer.exclude_ip_from_identifiers' => true]); + + $logFile = new LogFile($path); + + expect($logFile->identifier)->toBe( + Utils::shortMd5($path).'-laravel.log' + )->and($logFile->subFolderIdentifier())->toBe( + Utils::shortMd5($logFile->subFolder) + ); + + // Reset config for other tests + config(['log-viewer.exclude_ip_from_identifiers' => false]); +}); diff --git a/tests/Unit/LogFolderTest.php b/tests/Unit/LogFolderTest.php index 1b807fdb..3479b53f 100644 --- a/tests/Unit/LogFolderTest.php +++ b/tests/Unit/LogFolderTest.php @@ -34,3 +34,20 @@ Utils::shortMd5($serverIp.':'.$folder->path) ); }); + +test('log folder identifier excludes IP when config is enabled', function () { + // Set the cached local IP to a known value: + Utils::setCachedLocalIP($serverIp = '123.123.123.123'); + + // Enable the config to exclude IP from identifiers + config(['log-viewer.exclude_ip_from_identifiers' => true]); + + $folder = new LogFolder('folder', []); + + expect($folder->identifier)->toBe( + Utils::shortMd5($folder->path) + ); + + // Reset config for other tests + config(['log-viewer.exclude_ip_from_identifiers' => false]); +});