35 lines
978 B
PHP
Executable File
35 lines
978 B
PHP
Executable File
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use Illuminate\Console\Command;
|
|
|
|
class CleanLogs extends Command
|
|
{
|
|
protected $signature = 'clean_logs {--days=7 : Number of days before which logs should be deleted}';
|
|
|
|
protected $description = 'Clean Laravel log files older than the specified number of days';
|
|
|
|
public function handle()
|
|
{
|
|
$days = $this->option('days');
|
|
$dateThreshold = now()->subDays($days)->format('Y-m-d');
|
|
|
|
$logFiles = glob(storage_path('logs/*.log'));
|
|
|
|
foreach ($logFiles as $logFile) {
|
|
$cmd = "grep -n -E '^\[$dateThreshold|^$dateThreshold' $logFile | cut -d ':' -f 1";
|
|
$line = exec($cmd);
|
|
if ($line) {
|
|
$line = (int) $line;
|
|
if ($line > 1) {
|
|
$line -= 1;
|
|
}
|
|
exec("sed -i '1," . $line . "d' $logFile");
|
|
}
|
|
}
|
|
|
|
$this->info("$dateThreshold - Log cleanup completed.");
|
|
}
|
|
}
|