AutoClearLog.php 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Models\NodeDailyDataFlow;
  4. use App\Models\NodeHeartbeat;
  5. use App\Models\NodeHourlyDataFlow;
  6. use App\Models\NodeOnlineIp;
  7. use App\Models\NodeOnlineLog;
  8. use App\Models\NotificationLog;
  9. use App\Models\Payment;
  10. use App\Models\RuleLog;
  11. use App\Models\UserBanedLog;
  12. use App\Models\UserDailyDataFlow;
  13. use App\Models\UserDataFlowLog;
  14. use App\Models\UserHourlyDataFlow;
  15. use App\Models\UserLoginLog;
  16. use App\Models\UserSubscribeLog;
  17. use Exception;
  18. use Illuminate\Console\Command;
  19. use Log;
  20. class AutoClearLog extends Command
  21. {
  22. protected $signature = 'autoClearLog';
  23. protected $description = '自动清除日志';
  24. public function handle(): void
  25. {
  26. $jobStartTime = microtime(true);
  27. // 清除日志
  28. if (sysConfig('is_clear_log')) {
  29. $this->clearLog();
  30. }
  31. $jobEndTime = microtime(true);
  32. $jobUsedTime = round(($jobEndTime - $jobStartTime), 4);
  33. Log::info('---【'.$this->description.'】完成---,耗时'.$jobUsedTime.'秒');
  34. }
  35. // 清除日志
  36. private function clearLog(): void
  37. {
  38. try {
  39. // 清除节点每天流量数据日志
  40. NodeDailyDataFlow::where('created_at', '<=', date('Y-m-d H:i:s', strtotime('-2 month')))->delete();
  41. // 清除节点每小时流量数据日志
  42. NodeHourlyDataFlow::where('created_at', '<=', date('Y-m-d H:i:s', strtotime('-3 days')))->delete();
  43. // 清理通知日志
  44. NotificationLog::where('updated_at', '<=', date('Y-m-d H:i:s', strtotime('-1 month')))->delete();
  45. // 清除节点负载信息日志
  46. NodeHeartbeat::where('log_time', '<=', strtotime('-30 minutes'))->delete();
  47. // 清除节点在线用户数日志
  48. NodeOnlineLog::where('log_time', '<=', strtotime('-1 hour'))->delete();
  49. // 清理在线支付日志
  50. Payment::where('created_at', '<=', date('Y-m-d H:i:s', strtotime('-1 year')))->delete();
  51. // 清理审计触发日志
  52. RuleLog::where('created_at', '<=', date('Y-m-d H:i:s', strtotime('-3 month')))->delete();
  53. // 清除用户连接IP
  54. NodeOnlineIp::where('created_at', '<=', strtotime('-1 day'))->delete();
  55. // 清除用户封禁日志
  56. UserBanedLog::where('created_at', '<=', date('Y-m-d H:i:s', strtotime('-3 month')))->delete();
  57. // 清除用户各节点 / 节点总计的每天流量数据日志
  58. UserDailyDataFlow::where('node_id', '<>')
  59. ->where('created_at', '<=', date('Y-m-d H:i:s', strtotime('-1 month')))
  60. ->orWhere('created_at', '<=', date('Y-m-d H:i:s', strtotime('-3 month')))
  61. ->delete();
  62. // 清除用户每时各流量数据日志
  63. UserHourlyDataFlow::where('created_at', '<=', date('Y-m-d H:i:s', strtotime('-3 days')))->delete();
  64. // 清除用户登陆日志
  65. UserLoginLog::where('created_at', '<=', date('Y-m-d H:i:s', strtotime('-3 month')))->delete(); // 清除用户订阅记录
  66. // 清理用户订阅请求日志
  67. UserSubscribeLog::where('request_time', '<=', date('Y-m-d H:i:s', strtotime('-1 month')))->delete();
  68. // 清除用户流量日志
  69. UserDataFlowLog::where('log_time', '<=', strtotime('-3 days'))->delete();
  70. } catch (Exception $e) {
  71. Log::error('【清理日志】错误: '.$e->getMessage());
  72. }
  73. }
  74. }