Browse Source

feat(logging): 优化日志清理命令并支持配置文件设置

- 修改命令行参数,支持不传入 days 参数时使用配置文件中的值
- 新增 getDefaultDays 方法,从配置文件中读取保留天数
- 更新命令执行逻辑,优先使用配置文件中的保留天数
- 修改 README 文档,反映新的日志清理机制
- 调整配置文件中的默认保留天数为 3 天
AI Assistant 6 months ago
parent
commit
ef26ad8b31
3 changed files with 34 additions and 8 deletions
  1. 31 5
      UCore/Commands/CleanSizeRotatingLogsCommand.php
  2. 2 2
      UCore/Logging/README.md
  3. 1 1
      config/logging.php

+ 31 - 5
UCore/Commands/CleanSizeRotatingLogsCommand.php

@@ -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) {

+ 2 - 2
UCore/Logging/README.md

@@ -111,7 +111,7 @@ UCore 提供了自动清理过期日志文件的功能:
 
 ### 清理命令
 ```bash
-# 清理超过6天的备份文件(默认
+# 使用配置文件中的保留天数(默认读取 size_rotating_daily.days 配置
 php artisan ucore:clean-size-rotating-logs
 
 # 自定义保留天数
@@ -122,7 +122,7 @@ php artisan ucore:clean-size-rotating-logs --dry-run
 ```
 
 ### 自动计划任务
-系统会自动在每天凌晨3点执行清理任务,保留6天的备份文件
+系统会自动在每天凌晨3点执行清理任务,使用 `size_rotating_daily.days` 配置的保留天数
 
 ## 性能考虑
 

+ 1 - 1
config/logging.php

@@ -78,7 +78,7 @@ return [
             'driver' => 'size_rotating_daily',
             'path' => storage_path('logs/laravel.log'),
             'level' => env('LOG_LEVEL', 'debug'),
-            'days' => env('LOG_DAILY_DAYS', 14),
+            'days' => env('LOG_DAILY_DAYS', default: 3),
             'max_file_size' => env('LOG_MAX_FILE_SIZE', '100K'),
             'permission' => 0777,
             'locking' => false,