Skip to content

Commit e553f63

Browse files
committed
ability to configure whether local IP is used as part of file/folder hash
1 parent 871b7ac commit e553f63

File tree

5 files changed

+69
-3
lines changed

5 files changed

+69
-3
lines changed

config/log-viewer.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,19 @@
292292

293293
],
294294

295+
/*
296+
|--------------------------------------------------------------------------
297+
| Exclude IP from identifiers
298+
|--------------------------------------------------------------------------
299+
| By default, file and folder identifiers include the server's IP address
300+
| to ensure uniqueness. In load-balanced environments with shared storage,
301+
| this can cause "No results" errors. Set to true to exclude IP addresses
302+
| from identifier generation for consistent results across servers.
303+
|
304+
*/
305+
306+
'exclude_ip_from_identifiers' => env('LOG_VIEWER_EXCLUDE_IP_FROM_IDENTIFIERS', false),
307+
295308
/*
296309
|--------------------------------------------------------------------------
297310
| Root folder prefix

src/LogFile.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,13 @@ public function __construct(string $path, ?string $type = null, ?string $pathAli
2929
{
3030
$this->path = $path;
3131
$this->name = basename($path);
32-
$this->identifier = Utils::shortMd5(Utils::getLocalIP().':'.$path).'-'.$this->name;
32+
33+
if (config('log-viewer.exclude_ip_from_identifiers', false)) {
34+
$this->identifier = Utils::shortMd5($path).'-'.$this->name;
35+
} else {
36+
$this->identifier = Utils::shortMd5(Utils::getLocalIP().':'.$path).'-'.$this->name;
37+
}
38+
3339
$this->type = $type;
3440
$this->displayPath = empty($pathAlias)
3541
? $path
@@ -102,7 +108,11 @@ public function sizeFormatted(): string
102108

103109
public function subFolderIdentifier(): string
104110
{
105-
return Utils::shortMd5(Utils::getLocalIP().':'.$this->subFolder);
111+
if (config('log-viewer.exclude_ip_from_identifiers', false)) {
112+
return Utils::shortMd5($this->subFolder);
113+
} else {
114+
return Utils::shortMd5(Utils::getLocalIP().':'.$this->subFolder);
115+
}
106116
}
107117

108118
public function downloadUrl(): string

src/LogFolder.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@ public function __construct(
1717
public string $path,
1818
mixed $files,
1919
) {
20-
$this->identifier = Utils::shortMd5(Utils::getLocalIP().':'.$path);
20+
if (config('log-viewer.exclude_ip_from_identifiers', false)) {
21+
$this->identifier = Utils::shortMd5($path);
22+
} else {
23+
$this->identifier = Utils::shortMd5(Utils::getLocalIP().':'.$path);
24+
}
25+
2126
$this->files = new LogFileCollection($files);
2227
}
2328

tests/Unit/LogFileTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,24 @@
3939
Utils::shortMd5($serverIp.':'.$logFile->subFolder)
4040
);
4141
});
42+
43+
test('log file identifier excludes IP when config is enabled', function () {
44+
$path = storage_path('logs/laravel.log');
45+
file_put_contents($path, str_repeat('0', 10)); // 10 bytes
46+
// Set the cached local IP to a known value:
47+
Utils::setCachedLocalIP($serverIp = '123.123.123.123');
48+
49+
// Enable the config to exclude IP from identifiers
50+
config(['log-viewer.exclude_ip_from_identifiers' => true]);
51+
52+
$logFile = new LogFile($path);
53+
54+
expect($logFile->identifier)->toBe(
55+
Utils::shortMd5($path).'-laravel.log'
56+
)->and($logFile->subFolderIdentifier())->toBe(
57+
Utils::shortMd5($logFile->subFolder)
58+
);
59+
60+
// Reset config for other tests
61+
config(['log-viewer.exclude_ip_from_identifiers' => false]);
62+
});

tests/Unit/LogFolderTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,20 @@
3434
Utils::shortMd5($serverIp.':'.$folder->path)
3535
);
3636
});
37+
38+
test('log folder identifier excludes IP when config is enabled', function () {
39+
// Set the cached local IP to a known value:
40+
Utils::setCachedLocalIP($serverIp = '123.123.123.123');
41+
42+
// Enable the config to exclude IP from identifiers
43+
config(['log-viewer.exclude_ip_from_identifiers' => true]);
44+
45+
$folder = new LogFolder('folder', []);
46+
47+
expect($folder->identifier)->toBe(
48+
Utils::shortMd5($folder->path)
49+
);
50+
51+
// Reset config for other tests
52+
config(['log-viewer.exclude_ip_from_identifiers' => false]);
53+
});

0 commit comments

Comments
 (0)