|
|
@@ -8,10 +8,10 @@ use Carbon\Carbon;
|
|
|
|
|
|
/**
|
|
|
* 清理 size_rotating_daily 日志文件的计划任务
|
|
|
- *
|
|
|
+ *
|
|
|
* 功能:
|
|
|
* 1. 删除超过指定天数的日志备份文件
|
|
|
- * 2. 支持自定义保留天数(默认6天)
|
|
|
+ * 2. 自动读取 size_rotating_daily.days 配置作为默认保留天数
|
|
|
* 3. 提供详细的清理报告
|
|
|
*/
|
|
|
class CleanSizeRotatingLogsCommand extends Command
|
|
|
@@ -21,7 +21,7 @@ class CleanSizeRotatingLogsCommand extends Command
|
|
|
*
|
|
|
* @var string
|
|
|
*/
|
|
|
- protected $signature = 'ucore:clean-size-rotating-logs {--days=6 : 保留的天数} {--dry-run : 仅显示将要删除的文件,不实际删除}';
|
|
|
+ protected $signature = 'ucore:clean-size-rotating-logs {--days= : 保留的天数,默认使用 size_rotating_daily.days 配置} {--dry-run : 仅显示将要删除的文件,不实际删除}';
|
|
|
|
|
|
/**
|
|
|
* 命令描述
|
|
|
@@ -46,6 +46,18 @@ class CleanSizeRotatingLogsCommand extends Command
|
|
|
$this->logBackupPath = storage_path('logs/size_rotating_daily');
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 获取默认保留天数
|
|
|
+ * 从 size_rotating_daily 日志配置中读取 days 参数
|
|
|
+ *
|
|
|
+ * @return int
|
|
|
+ */
|
|
|
+ protected function getDefaultDays(): int
|
|
|
+ {
|
|
|
+ $loggingConfig = config('logging.channels.size_rotating_daily', []);
|
|
|
+ return $loggingConfig['days'] ?? 7; // 如果配置不存在,默认7天
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 执行命令
|
|
|
*
|
|
|
@@ -53,11 +65,25 @@ class CleanSizeRotatingLogsCommand extends Command
|
|
|
*/
|
|
|
public function handle(): int
|
|
|
{
|
|
|
- $days = (int) $this->option('days');
|
|
|
+ // 如果没有指定 days 参数,使用配置文件中的值
|
|
|
+ $days = $this->option('days');
|
|
|
+ if ($days === null) {
|
|
|
+ $days = $this->getDefaultDays();
|
|
|
+ } else {
|
|
|
+ $days = (int) $days;
|
|
|
+ }
|
|
|
+
|
|
|
$dryRun = $this->option('dry-run');
|
|
|
|
|
|
$this->info("开始清理 size_rotating_daily 日志文件...");
|
|
|
- $this->info("保留天数: {$days} 天");
|
|
|
+
|
|
|
+ // 显示配置来源
|
|
|
+ if ($this->option('days') === null) {
|
|
|
+ $this->info("保留天数: {$days} 天 (来自 size_rotating_daily.days 配置)");
|
|
|
+ } else {
|
|
|
+ $this->info("保留天数: {$days} 天 (命令行参数)");
|
|
|
+ }
|
|
|
+
|
|
|
$this->info("备份目录: {$this->logBackupPath}");
|
|
|
|
|
|
if ($dryRun) {
|