Skip to content

Commit

Permalink
Merge pull request #257 from opcodesio/hide-unknown-log-files
Browse files Browse the repository at this point in the history
hide unknown logs from the UI & API calls
  • Loading branch information
arukompas authored Aug 14, 2023
2 parents 974e63d + ee689e4 commit 813cfa6
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 1 deletion.
12 changes: 12 additions & 0 deletions config/log-viewer.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,18 @@
// 'my_secret.log'
],

/*
|--------------------------------------------------------------------------
| Hide unknown files.
|--------------------------------------------------------------------------
| The include/exclude options above might catch files which are not
| logs supported by Log Viewer. In that case, you can hide them
| from the UI and API calls by setting this to true.
|
*/

'hide_unknown_files' => true,

/*
|--------------------------------------------------------------------------
| Shorter stack trace filters.
Expand Down
6 changes: 6 additions & 0 deletions src/LogViewerService.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ public function getFiles(): LogFileCollection
->unique()
->map(fn ($filePath) => new $fileClass($filePath))
->values();

if (config('log-viewer.hide_unknown_files', true)) {
$this->_cachedFiles = $this->_cachedFiles->filter(function (LogFile $file) {
return ! $file->type()->isUnknown();
});
}
}

return $this->_cachedFiles;
Expand Down
5 changes: 5 additions & 0 deletions src/Logs/LogType.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,9 @@ public function logClass(): ?string
{
return app(LogTypeRegistrar::class)->getClass($this->value);
}

public function isUnknown(): bool
{
return $this->value === static::DEFAULT;
}
}
1 change: 1 addition & 0 deletions tests/Feature/ClearingFileCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use function PHPUnit\Framework\assertNotSame;

beforeEach(function () {
config(['log-viewer.hide_unknown_files' => false]);
generateLogFiles(['laravel.log', 'other.log']);
$this->file = LogViewer::getFile('laravel.log');
$this->otherFile = LogViewer::getFile('other.log');
Expand Down
13 changes: 12 additions & 1 deletion tests/Feature/LogViewerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use function PHPUnit\Framework\assertNotContains;

beforeEach(function () {
generateLogFiles(['laravel.log', 'other.log']);
generateLogFiles(['laravel.log', 'other.log'], randomContent: true, type: 'laravel');
});

it('properly includes log files', function () {
Expand All @@ -23,3 +23,14 @@
assertContains('laravel.log', $fileNames);
assertNotContains('other.log', $fileNames);
});

it('hides unknown log files', function () {
config()->set('log-viewer.hide_unknown_files', true);
$unknownFile = generateLogFile('unknown.log', content: 'unknown log content');

$fileNames = LogViewer::getFiles()->map->name;

assertNotContains($unknownFile->name, $fileNames);
assertContains('laravel.log', $fileNames);
assertContains('other.log', $fileNames);
});
5 changes: 5 additions & 0 deletions tests/Unit/FilePathsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
use Opcodes\LogViewer\Facades\LogViewer;
use Opcodes\LogViewer\LogViewerService;

beforeEach(function () {
// irrelevant option for these tests, so let's show all files.
config(['log-viewer.hide_unknown_files' => false]);
});

test('handles square brackets in the logs path', function ($folderPath) {
// Get the original path inside which we'll create a dummy folder with square brackets
$originalBasePath = LogViewer::basePathForLogs();
Expand Down

0 comments on commit 813cfa6

Please sign in to comment.