AutoClearLog.php 3.0 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 __construct() {
  22. parent::__construct();
  23. }
  24. public function handle() {
  25. $jobStartTime = microtime(true);
  26. // 清除日志
  27. if(Helpers::systemConfig()['is_clear_log']){
  28. $this->clearLog();
  29. }
  30. $jobEndTime = microtime(true);
  31. $jobUsedTime = round(($jobEndTime - $jobStartTime), 4);
  32. Log::info('---【'.$this->description.'】完成---,耗时'.$jobUsedTime.'秒');
  33. }
  34. // 清除日志
  35. private function clearLog() {
  36. try{
  37. // 自动清除30分钟以前的节点负载信息日志
  38. SsNodeInfo::query()->where('log_time', '<=', strtotime("-30 minutes"))->delete();
  39. // 自动清除1小时以前的节点在线用户数日志
  40. SsNodeOnlineLog::query()->where('log_time', '<=', strtotime("-1 hour"))->delete();
  41. // 自动清除3天以前的用户流量日志
  42. UserTrafficLog::query()->where('log_time', '<=', strtotime("-3 days"))->delete();
  43. // 自动清除3天以前的用户每小时流量数据日志
  44. UserTrafficHourly::query()->where('created_at', '<=', date('Y-m-d H:i:s', strtotime('-3 days')))->delete();
  45. // 自动清除1个月以前的用户每天流量数据日志
  46. UserTrafficDaily::query()
  47. ->where('created_at', '<=', date('Y-m-d H:i:s', strtotime('-1 month 5 days')))
  48. ->delete();
  49. // 自动清除2个月以前的节点每小时流量数据日志
  50. SsNodeTrafficHourly::query()
  51. ->where('created_at', '<=', date('Y-m-d H:i:s', strtotime('-2 month')))
  52. ->delete();
  53. // 自动清除3个月以前的节点每天流量数据日志
  54. SsNodeTrafficDaily::query()
  55. ->where('created_at', '<=', date('Y-m-d H:i:s', strtotime('-3 month')))
  56. ->delete();
  57. // 自动清除30天以前用户封禁日志
  58. UserBanLog::query()->where('created_at', '<=', date('Y-m-d H:i:s', strtotime("-1 month")))->delete();
  59. // 自动清除1月前用户连接IP
  60. SsNodeIp::query()->where('created_at', '<=', strtotime("-1 month"))->delete();
  61. // 自动清除3个月以前用户登陆日志
  62. UserLoginLog::query()->where('created_at', '<=', date('Y-m-d H:i:s', strtotime("-3 month")))->delete();
  63. // 自动清除1个月前的用户订阅记录
  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. }