Skip to content

Commit 876166d

Browse files
authored
Merge pull request #416 from opcodesio/feature/renaming-and-grouping-multiple-includes
make it possible to group and rename multiple file includes
2 parents 605d32d + ce88508 commit 876166d

File tree

5 files changed

+99
-11
lines changed

5 files changed

+99
-11
lines changed

config/log-viewer.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,9 @@
142142
'**/*.log',
143143

144144
// You can include paths to other log types as well, such as apache, nginx, and more.
145-
'/var/log/httpd/*',
146-
'/var/log/nginx/*',
145+
// This key => value pair can be used to rename and group multiple paths into one folder in the UI.
146+
'/var/log/httpd/*' => 'Apache',
147+
'/var/log/nginx/*' => 'Nginx',
147148

148149
// MacOS Apple Silicon logs
149150
'/opt/homebrew/var/log/nginx/*',

src/LogFile.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,28 @@ class LogFile
2121
public string $name;
2222
public string $identifier;
2323
public string $subFolder = '';
24+
public string $displayPath;
2425
private ?string $type = null;
2526
private array $_logIndexCache;
2627

27-
public function __construct(string $path, ?string $type = null)
28+
public function __construct(string $path, ?string $type = null, ?string $pathAlias = null)
2829
{
2930
$this->path = $path;
3031
$this->name = basename($path);
3132
$this->identifier = Utils::shortMd5(Utils::getLocalIP().':'.$path).'-'.$this->name;
3233
$this->type = $type;
34+
$this->displayPath = empty($pathAlias)
35+
? $path
36+
: $pathAlias.DIRECTORY_SEPARATOR.$this->name;
3337

3438
// Let's remove the file name because we already know it.
3539
$this->subFolder = str_replace($this->name, '', $path);
3640
$this->subFolder = rtrim($this->subFolder, DIRECTORY_SEPARATOR);
3741

42+
if (! empty($pathAlias)) {
43+
$this->subFolder = $pathAlias;
44+
}
45+
3846
$this->loadMetadata();
3947
}
4048

src/LogViewerService.php

+46-8
Original file line numberDiff line numberDiff line change
@@ -61,26 +61,58 @@ protected function getLaravelLogFilePaths(): array
6161
);
6262
}
6363
$files = [];
64+
$filePathsCollected = [];
65+
66+
foreach (config('log-viewer.include_files', []) as $pattern => $alias) {
67+
if (is_numeric($pattern)) {
68+
$pattern = $alias;
69+
$alias = null;
70+
}
6471

65-
foreach (config('log-viewer.include_files', []) as $pattern) {
6672
if (! str_starts_with($pattern, DIRECTORY_SEPARATOR)) {
6773
$pattern = $baseDir.$pattern;
6874
}
6975

70-
$files = array_merge($files, $this->getFilePathsMatchingPattern($pattern));
76+
$filesMatchingPattern = $this->getFilePathsMatchingPattern($pattern);
77+
$filesMatchingPattern = array_map('realpath', $filesMatchingPattern);
78+
$filesMatchingPattern = array_values(array_filter($filesMatchingPattern, 'is_file'));
79+
$filesMatchingPattern = array_values(array_diff($filesMatchingPattern, $filePathsCollected));
80+
$filePathsCollected = array_merge($filePathsCollected, $filesMatchingPattern);
81+
82+
// Let's prep aliases if they are provided.
83+
if (! empty($alias)) {
84+
$filesMatchingPattern = array_map(fn ($path) => [$path, $alias], $filesMatchingPattern);
85+
}
86+
87+
$files = array_merge($files, $filesMatchingPattern);
7188
}
7289

73-
foreach (config('log-viewer.exclude_files', []) as $pattern) {
90+
foreach (config('log-viewer.exclude_files', []) as $pattern => $alias) {
91+
if (is_numeric($pattern)) {
92+
$pattern = $alias;
93+
$alias = null;
94+
}
95+
7496
if (! str_starts_with($pattern, DIRECTORY_SEPARATOR)) {
7597
$pattern = $baseDir.$pattern;
7698
}
7799

78-
$files = array_diff($files, $this->getFilePathsMatchingPattern($pattern));
79-
}
100+
$filesMatchingPattern = $this->getFilePathsMatchingPattern($pattern);
101+
$filesMatchingPattern = array_map('realpath', $filesMatchingPattern);
102+
$filesMatchingPattern = array_values(array_filter($filesMatchingPattern, 'is_file'));
103+
104+
if (! empty($alias)) {
105+
$filesMatchingPattern = array_map(fn ($path) => [$path, $alias], $filesMatchingPattern);
106+
}
80107

81-
$files = array_map('realpath', $files);
108+
$files = array_filter($files, function (string|array $file) use ($filesMatchingPattern) {
109+
if (is_array($file)) {
110+
return ! in_array($file[0], $filesMatchingPattern);
111+
}
82112

83-
$files = array_filter($files, 'is_file');
113+
return ! in_array($file, $filesMatchingPattern);
114+
});
115+
}
84116

85117
return array_values(array_reverse($files));
86118
}
@@ -111,7 +143,13 @@ public function getFiles(): LogFileCollection
111143

112144
$this->_cachedFiles = (new LogFileCollection($this->getLaravelLogFilePaths()))
113145
->unique()
114-
->map(fn ($filePath) => new $fileClass($filePath))
146+
->map(function ($filePath) use ($fileClass) {
147+
if (is_array($filePath)) {
148+
[$filePath, $alias] = $filePath;
149+
}
150+
151+
return new $fileClass($filePath, null, $alias ?? null);
152+
})
115153
->values();
116154

117155
if (config('log-viewer.hide_unknown_files', true)) {

tests/Feature/LogViewerTest.php

+10
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@
2525
assertNotContains('other.log', $fileNames);
2626
});
2727

28+
it('properly excludes log files added by aliased include', function () {
29+
config()->set('log-viewer.include_files', ['*.log' => 'Alias']);
30+
config()->set('log-viewer.exclude_files', ['*other*']);
31+
32+
$fileNames = LogViewer::getFiles()->map->name;
33+
34+
assertContains('laravel.log', $fileNames);
35+
assertNotContains('other.log', $fileNames);
36+
});
37+
2838
it('hides unknown log files', function () {
2939
config()->set('log-viewer.hide_unknown_files', true);
3040
$unknownFile = generateLogFile('unknown.log', content: 'unknown log content');

tests/Unit/FilePathsTest.php

+31
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,34 @@
108108
->and($files->contains('name', $second->name))->toBeFalse()
109109
->and(file_exists($files[0]->path))->toBeTrue();
110110
});
111+
112+
test('can set aliases for paths', function () {
113+
config(['log-viewer.include_files' => [
114+
'alias/folder/*.log' => 'TestPath', // equals to "storage/logs/alias/folder/*.log"
115+
]]);
116+
$originalFile = generateLogFile('alias/folder/first.log');
117+
118+
$files = LogViewer::getFiles();
119+
120+
expect($files)->toHaveCount(1);
121+
$file = $files[0];
122+
expect($file->path)->toBe($originalFile->path)
123+
->and($file->displayPath)->toBe('TestPath/first.log')
124+
->and($file->subFolder)->toBe('TestPath');
125+
});
126+
127+
test('shows unique files even with aliases used for paths', function () {
128+
config(['log-viewer.include_files' => [
129+
'alias/folder/*.log' => 'TestPath', // equals to "storage/logs/alias/folder/*.log"
130+
'alias/**/*.log',
131+
]]);
132+
$originalFile = generateLogFile('alias/folder/first.log');
133+
134+
$files = LogViewer::getFiles();
135+
136+
expect($files)->toHaveCount(1);
137+
$file = $files[0];
138+
expect($file->path)->toBe($originalFile->path)
139+
->and($file->displayPath)->toBe('TestPath/first.log')
140+
->and($file->subFolder)->toBe('TestPath');
141+
});

0 commit comments

Comments
 (0)