Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hide unknown logs from the UI & API calls #257

Merged
merged 2 commits into from
Aug 14, 2023
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
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