Skip to content

Commit 813cfa6

Browse files
authored
Merge pull request #257 from opcodesio/hide-unknown-log-files
hide unknown logs from the UI & API calls
2 parents 974e63d + ee689e4 commit 813cfa6

File tree

6 files changed

+41
-1
lines changed

6 files changed

+41
-1
lines changed

config/log-viewer.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,18 @@
160160
// 'my_secret.log'
161161
],
162162

163+
/*
164+
|--------------------------------------------------------------------------
165+
| Hide unknown files.
166+
|--------------------------------------------------------------------------
167+
| The include/exclude options above might catch files which are not
168+
| logs supported by Log Viewer. In that case, you can hide them
169+
| from the UI and API calls by setting this to true.
170+
|
171+
*/
172+
173+
'hide_unknown_files' => true,
174+
163175
/*
164176
|--------------------------------------------------------------------------
165177
| Shorter stack trace filters.

src/LogViewerService.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,12 @@ public function getFiles(): LogFileCollection
101101
->unique()
102102
->map(fn ($filePath) => new $fileClass($filePath))
103103
->values();
104+
105+
if (config('log-viewer.hide_unknown_files', true)) {
106+
$this->_cachedFiles = $this->_cachedFiles->filter(function (LogFile $file) {
107+
return ! $file->type()->isUnknown();
108+
});
109+
}
104110
}
105111

106112
return $this->_cachedFiles;

src/Logs/LogType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,9 @@ public function logClass(): ?string
5656
{
5757
return app(LogTypeRegistrar::class)->getClass($this->value);
5858
}
59+
60+
public function isUnknown(): bool
61+
{
62+
return $this->value === static::DEFAULT;
63+
}
5964
}

tests/Feature/ClearingFileCacheTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use function PHPUnit\Framework\assertNotSame;
88

99
beforeEach(function () {
10+
config(['log-viewer.hide_unknown_files' => false]);
1011
generateLogFiles(['laravel.log', 'other.log']);
1112
$this->file = LogViewer::getFile('laravel.log');
1213
$this->otherFile = LogViewer::getFile('other.log');

tests/Feature/LogViewerTest.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use function PHPUnit\Framework\assertNotContains;
66

77
beforeEach(function () {
8-
generateLogFiles(['laravel.log', 'other.log']);
8+
generateLogFiles(['laravel.log', 'other.log'], randomContent: true, type: 'laravel');
99
});
1010

1111
it('properly includes log files', function () {
@@ -23,3 +23,14 @@
2323
assertContains('laravel.log', $fileNames);
2424
assertNotContains('other.log', $fileNames);
2525
});
26+
27+
it('hides unknown log files', function () {
28+
config()->set('log-viewer.hide_unknown_files', true);
29+
$unknownFile = generateLogFile('unknown.log', content: 'unknown log content');
30+
31+
$fileNames = LogViewer::getFiles()->map->name;
32+
33+
assertNotContains($unknownFile->name, $fileNames);
34+
assertContains('laravel.log', $fileNames);
35+
assertContains('other.log', $fileNames);
36+
});

tests/Unit/FilePathsTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
use Opcodes\LogViewer\Facades\LogViewer;
44
use Opcodes\LogViewer\LogViewerService;
55

6+
beforeEach(function () {
7+
// irrelevant option for these tests, so let's show all files.
8+
config(['log-viewer.hide_unknown_files' => false]);
9+
});
10+
611
test('handles square brackets in the logs path', function ($folderPath) {
712
// Get the original path inside which we'll create a dummy folder with square brackets
813
$originalBasePath = LogViewer::basePathForLogs();

0 commit comments

Comments
 (0)