AutoClearLog.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Components\Helpers;
  4. use App\Http\Models\SsNodeIp;
  5. use App\Http\Models\SsNodeInfo;
  6. use App\Http\Models\SsNodeOnlineLog;
  7. use App\Http\Models\SsNodeTrafficHourly;
  8. use App\Http\Models\SsNodeTrafficDaily;
  9. use App\Http\Models\UserBanLog;
  10. use App\Http\Models\UserLoginLog;
  11. use App\Http\Models\UserTrafficDaily;
  12. use App\Http\Models\UserTrafficLog;
  13. use App\Http\Models\UserTrafficHourly;
  14. use Illuminate\Console\Command;
  15. use Log;
  16. class AutoClearLog extends Command
  17. {
  18. protected $signature = 'autoClearLog';
  19. protected $description = '自动清除日志';
  20. protected static $systemConfig;
  21. public function __construct()
  22. {
  23. parent::__construct();
  24. self::$systemConfig = Helpers::systemConfig();
  25. }
  26. public function handle()
  27. {
  28. $jobStartTime = microtime(true);
  29. // 清除日志
  30. if (self::$systemConfig['is_clear_log']) {
  31. $this->clearLog();
  32. }
  33. $jobEndTime = microtime(true);
  34. $jobUsedTime = round(($jobEndTime - $jobStartTime), 4);
  35. Log::info('执行定时任务【' . $this->description . '】,耗时' . $jobUsedTime . '秒');
  36. }
  37. // 清除日志
  38. private function clearLog()
  39. {
  40. // 自动清除30分钟以前的节点负载信息日志
  41. SsNodeInfo::query()->where('log_time', '<=', strtotime("-30 minutes"))->delete();
  42. // 自动清除1小时以前的节点在线用户数日志
  43. SsNodeOnlineLog::query()->where('log_time', '<=', strtotime("-1 hour"))->delete();
  44. // 自动清除1个月以前的用户流量日志
  45. UserTrafficLog::query()->where('log_time', '<=', strtotime("-1 month"))->delete();
  46. // 自动清除10天以前的用户每小时流量数据日志
  47. UserTrafficHourly::query()->where('created_at', '<=', date('Y-m-d H:i:s', strtotime('-10 days')))->delete();
  48. // 自动清除1个月以前的用户每天流量数据日志
  49. UserTrafficDaily::query()->where('created_at', '<=', date('Y-m-d H:i:s', strtotime('-1 month')))->delete();
  50. // 自动清除2个月以前的节点每小时流量数据日志
  51. SsNodeTrafficHourly::query()->where('created_at', '<=', date('Y-m-d H:i:s', strtotime('-2 month')))->delete();
  52. // 自动清除3个月以前的节点每天流量数据日志
  53. SsNodeTrafficDaily::query()->where('created_at', '<=', date('Y-m-d H:i:s', strtotime('-3 month')))->delete();
  54. // 自动清除30天以前用户封禁日志
  55. UserBanLog::query()->where('created_at', '<=', date('Y-m-d H:i:s', strtotime("-1 month")))->delete();
  56. // 自动清除1个月以前用户连接IP
  57. SsNodeIp::query()->where('created_at', '<=', strtotime("-1 month"))->delete();
  58. // 自动清除3个月以前用户登陆日志
  59. UserLoginLog::query()->where('created_at', '<=', date('Y-m-d H:i:s', strtotime("-3 month")))->delete();
  60. }
  61. }