setName('api_log') ->setDescription('the api_log command'); } /** * Execute the console command. * * @param Input $input * @param Output $output * @return void */ protected function execute(Input $input, Output $output) { while (true) { try { $redis = Cache::store('redis')->handler(); $result = $redis->blpop(['api_log'], 5); if ($result) { $logData = json_decode($result[1], true); if (!is_array($logData) || empty($logData)) { continue; } if ($logData) { $logData['params'] = json_encode($logData['params'] ?? [], JSON_UNESCAPED_UNICODE); $logData['response'] = json_encode($logData['response'] ?? [], JSON_UNESCAPED_UNICODE); ApiLogModel::create($logData); } } else { // 如果没有数据,休眠一段时间再继续 sleep(10); } } catch (\Throwable $e) { Log::error('ApiLogCommand error: ' . $e->getMessage()); sleep(3); } } } }