AutoClearLog.php 2.8 KB

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