| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- <?php
- namespace App\Module\Farm\Commands;
- use App\Module\Farm\Logics\HarvestLogLogic;
- use App\Module\Farm\Logics\UpgradeLogLogic;
- use Illuminate\Console\Command;
- use Illuminate\Support\Facades\Log;
- /**
- * 清理过期日志命令
- */
- class CleanExpiredLogsCommand extends Command
- {
- /**
- * 命令名称
- *
- * @var string
- */
- protected $signature = 'farm:clean-expired-logs {--days=90 : 保留天数}';
- /**
- * 命令描述
- *
- * @var string
- */
- protected $description = '清理过期的日志记录';
- /**
- * 执行命令
- *
- * @return int
- */
- public function handle()
- {
- $days = $this->option('days');
- $this->info("开始清理 {$days} 天前的日志记录...");
- try {
- // 清理收获记录
- $harvestLogLogic = new HarvestLogLogic();
- $harvestCount = $harvestLogLogic->cleanupOldLogs($days);
- $this->info("清理了 {$harvestCount} 条收获记录");
- // 清理升级记录
- $upgradeLogLogic = new UpgradeLogLogic();
- $upgradeCount = $upgradeLogLogic->cleanupOldLogs($days);
- $this->info("清理了 {$upgradeCount} 条升级记录");
- $totalCount = $harvestCount + $upgradeCount ;
- $this->info("共清理了 {$totalCount} 条日志记录");
- Log::info('日志清理成功', [
- 'days' => $days,
- 'harvest_count' => $harvestCount,
- 'upgrade_count' => $upgradeCount,
- 'total_count' => $totalCount
- ]);
- return 0;
- } catch (\Exception $e) {
- $this->error('日志清理失败: ' . $e->getMessage());
- Log::error('日志清理失败', [
- 'days' => $days,
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
- return 1;
- }
- }
- }
|