AutoClearLog.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 Exception;
  16. use Illuminate\Console\Command;
  17. use Log;
  18. class AutoClearLog extends Command
  19. {
  20. protected $signature = 'autoClearLog';
  21. protected $description = '自动清除日志';
  22. public function __construct()
  23. {
  24. parent::__construct();
  25. }
  26. public function handle()
  27. {
  28. $jobStartTime = microtime(TRUE);
  29. // 清除日志
  30. if(Helpers::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. try{
  42. SsNodeInfo::query()->where('log_time', '<=', strtotime("-30 minutes"))->delete();
  43. // 自动清除1小时以前的节点在线用户数日志
  44. SsNodeOnlineLog::query()->where('log_time', '<=', strtotime("-1 hour"))->delete();
  45. // 自动清除3天以前的用户流量日志
  46. UserTrafficLog::query()->where('log_time', '<=', strtotime("-3 days"))->delete();
  47. // 自动清除3天以前的用户每小时流量数据日志
  48. UserTrafficHourly::query()->where('created_at', '<=', date('Y-m-d H:i:s', strtotime('-3 days')))->delete();
  49. // 自动清除1个月以前的用户每天流量数据日志
  50. UserTrafficDaily::query()->where('created_at', '<=', date('Y-m-d H:i:s', strtotime('-1 month 5 days')))->delete();
  51. // 自动清除2个月以前的节点每小时流量数据日志
  52. SsNodeTrafficHourly::query()->where('created_at', '<=', date('Y-m-d H:i:s', strtotime('-2 month')))->delete();
  53. // 自动清除3个月以前的节点每天流量数据日志
  54. SsNodeTrafficDaily::query()->where('created_at', '<=', date('Y-m-d H:i:s', strtotime('-3 month')))->delete();
  55. // 自动清除30天以前用户封禁日志
  56. UserBanLog::query()->where('created_at', '<=', date('Y-m-d H:i:s', strtotime("-1 month")))->delete();
  57. // 自动清除1月前用户连接IP
  58. SsNodeIp::query()->where('created_at', '<=', strtotime("-1 month"))->delete();
  59. // 自动清除3个月以前用户登陆日志
  60. UserLoginLog::query()->where('created_at', '<=', date('Y-m-d H:i:s', strtotime("-3 month")))->delete();
  61. // 自动清除1个月前的用户订阅记录
  62. UserSubscribeLog::query()->where('request_time', '<=', date('Y-m-d H:i:s', strtotime("-1 month")))->delete();
  63. }catch(Exception $e){
  64. Log::error('【清理日志】错误: '.$e->getMessage());
  65. }
  66. }
  67. }