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
13 changes: 13 additions & 0 deletions config/log-viewer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 12 additions & 2 deletions src/LogFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion src/LogFolder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
21 changes: 21 additions & 0 deletions tests/Unit/LogFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
});
17 changes: 17 additions & 0 deletions tests/Unit/LogFolderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
});
Loading