From 9c3de7a7599b5414ef628f24048b2ebdc366d6c9 Mon Sep 17 00:00:00 2001 From: adhityairvan Date: Wed, 4 Jan 2023 11:13:43 +0700 Subject: [PATCH 1/3] add new option to keep specified file names --- README.md | 8 ++++++++ src/ClearLogs.php | 18 +++++++++++++++++- tests/ClearLogsTest.php | 14 ++++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 7ddefe9..e29a816 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,14 @@ Run this command to clear all log files except the last one in the log directory php artisan log:clear --keep-last ``` +### Clear all log files except specified file names + +Run this command to clear all log files except the specified files in the option. Specify file name without extension. Can be combined with `--keep-last` option. + +``` +php artisan log:clear --keep="keptLog" +``` + ### Automated cleanup of log files Add this to the App\Console\Kernel schedule method to ensure daily cleanup of old log files diff --git a/src/ClearLogs.php b/src/ClearLogs.php index da894d5..9373c96 100644 --- a/src/ClearLogs.php +++ b/src/ClearLogs.php @@ -13,7 +13,7 @@ class ClearLogs extends Command * * @var string */ - protected $signature = 'log:clear {--keep-last : Whether the last log file should be kept}'; + protected $signature = 'log:clear {--keep-last : Whether the last log file should be kept} {--keep=* : Log files to keep (without extension)}'; /** * The console command description. @@ -38,11 +38,14 @@ public function __construct(private Filesystem $disk) public function handle(): void { $files = $this->getLogFiles(); + $filesToKeep = $this->option('keep'); if ($this->option('keep-last') && $files->count() >= 1) { $files->shift(); } + $files = $this->filesToDelete($files, $filesToKeep); + $deleted = $this->delete($files); if (! $deleted) { @@ -64,6 +67,19 @@ private function getLogFiles(): Collection )->sortBy('mtime'); } + /** + * Remove specified files from deletion list + * + * @param Collection $files + * @return Collection + */ + private function filesToDelete(Collection $files, array $fileNames): Collection + { + return $files->filter(function($value, $key) use ($fileNames){ + return !in_array($value->getFilenameWithoutExtension(), $fileNames); + }); + } + /** * Delete the given files. */ diff --git a/tests/ClearLogsTest.php b/tests/ClearLogsTest.php index fef727f..85105d2 100644 --- a/tests/ClearLogsTest.php +++ b/tests/ClearLogsTest.php @@ -63,6 +63,20 @@ public function it_should_keep_the_last_log_file_if_the_option_is_with_only_one_ $this->assertFileExists($this->logDirectory . '/file1.log'); } + /** @test */ + public function it_should_keep_the_specified_log_file_if_the_option_is_keep_specified_files(): void + { + touch($this->logDirectory . '/file1.log', time() - 3600); + touch($this->logDirectory . '/file2.log', time() - 3600); + + $this->assertFileExists($this->logDirectory . '/file1.log'); + $this->assertFileExists($this->logDirectory . '/file2.log'); + + $this->artisan('log:clear', ['--keep' => ['file2']]); + + $this->assertFileExists($this->logDirectory . '/file2.log'); + } + /** @test */ public function it_should_return_zero_even_if_there_is_no_log_file(): void { From 49251f3b4acba673e56f02a920d41d84a973df88 Mon Sep 17 00:00:00 2001 From: hedii Date: Mon, 20 Feb 2023 08:18:48 +0100 Subject: [PATCH 2/3] Update ClearLogsTest.php --- tests/ClearLogsTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/ClearLogsTest.php b/tests/ClearLogsTest.php index 85105d2..411e908 100644 --- a/tests/ClearLogsTest.php +++ b/tests/ClearLogsTest.php @@ -74,6 +74,7 @@ public function it_should_keep_the_specified_log_file_if_the_option_is_keep_spec $this->artisan('log:clear', ['--keep' => ['file2']]); + $this->assertFileDoesNotExist($this->logDirectory . '/file1.log'); $this->assertFileExists($this->logDirectory . '/file2.log'); } From 7daec32c497c410a59bae51d4f7abbc63e89abb3 Mon Sep 17 00:00:00 2001 From: hedii Date: Mon, 20 Feb 2023 08:22:27 +0100 Subject: [PATCH 3/3] Update ClearLogs.php --- src/ClearLogs.php | 31 ++++++------------------------- 1 file changed, 6 insertions(+), 25 deletions(-) diff --git a/src/ClearLogs.php b/src/ClearLogs.php index 9373c96..2adec63 100644 --- a/src/ClearLogs.php +++ b/src/ClearLogs.php @@ -8,34 +8,16 @@ class ClearLogs extends Command { - /** - * The name and signature of the console command. - * - * @var string - */ protected $signature = 'log:clear {--keep-last : Whether the last log file should be kept} {--keep=* : Log files to keep (without extension)}'; - /** - * The console command description. - * - * @var string - */ protected $description = 'Remove every log files in the log directory'; - /** - * Create a new command instance. - * - * @param \Illuminate\Filesystem\Filesystem $disk - */ public function __construct(private Filesystem $disk) { parent::__construct(); } - /** - * Execute the console command. - */ - public function handle(): void + public function handle(): int { $files = $this->getLogFiles(); $filesToKeep = $this->option('keep'); @@ -55,6 +37,8 @@ public function handle(): void } else { $this->info($deleted . ' log files have been deleted'); } + + return Command::SUCCESS; } /** @@ -68,15 +52,12 @@ private function getLogFiles(): Collection } /** - * Remove specified files from deletion list - * - * @param Collection $files - * @return Collection + * Remove specified files from deletion list */ private function filesToDelete(Collection $files, array $fileNames): Collection { - return $files->filter(function($value, $key) use ($fileNames){ - return !in_array($value->getFilenameWithoutExtension(), $fileNames); + return $files->filter(function ($value, $key) use ($fileNames) { + return ! in_array($value->getFilenameWithoutExtension(), $fileNames); }); }