Skip to content

Commit

Permalink
Merge pull request #12 from adhityairvan/master
Browse files Browse the repository at this point in the history
add new option to keep specified file names
  • Loading branch information
hedii committed Feb 20, 2023
2 parents 4d7e251 + 7daec32 commit acc5961
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 20 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
37 changes: 17 additions & 20 deletions src/ClearLogs.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,26 @@

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}';
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');

if ($this->option('keep-last') && $files->count() >= 1) {
$files->shift();
}

$files = $this->filesToDelete($files, $filesToKeep);

$deleted = $this->delete($files);

if (! $deleted) {
Expand All @@ -52,6 +37,8 @@ public function handle(): void
} else {
$this->info($deleted . ' log files have been deleted');
}

return Command::SUCCESS;
}

/**
Expand All @@ -64,6 +51,16 @@ private function getLogFiles(): Collection
)->sortBy('mtime');
}

/**
* 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);
});
}

/**
* Delete the given files.
*/
Expand Down
15 changes: 15 additions & 0 deletions tests/ClearLogsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,21 @@ 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->assertFileDoesNotExist($this->logDirectory . '/file1.log');
$this->assertFileExists($this->logDirectory . '/file2.log');
}

/** @test */
public function it_should_return_zero_even_if_there_is_no_log_file(): void
{
Expand Down

0 comments on commit acc5961

Please sign in to comment.