AutoClearLog.php 2.9 KB

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