|
@ -10,22 +10,15 @@ use think\console\Input; |
|
|
use think\console\Output; |
|
|
use think\console\Output; |
|
|
use think\facade\Cache; |
|
|
use think\facade\Cache; |
|
|
use think\facade\Log; |
|
|
use think\facade\Log; |
|
|
|
|
|
use think\facade\Db; |
|
|
|
|
|
|
|
|
class ApiLogCommand extends Command |
|
|
class ApiLogCommand extends Command |
|
|
{ |
|
|
{ |
|
|
protected function configure() |
|
|
protected function configure() |
|
|
{ |
|
|
{ |
|
|
// 指令配置 |
|
|
$this->setName('api_log')->setDescription('the api_log command'); |
|
|
$this->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) |
|
|
protected function execute(Input $input, Output $output) |
|
|
{ |
|
|
{ |
|
|
while (true) { |
|
|
while (true) { |
|
@ -38,15 +31,25 @@ class ApiLogCommand extends Command |
|
|
if (!is_array($logData) || empty($logData)) { |
|
|
if (!is_array($logData) || empty($logData)) { |
|
|
continue; |
|
|
continue; |
|
|
} |
|
|
} |
|
|
if ($logData) { |
|
|
|
|
|
$logData['params'] = json_encode($logData['params'] ?? [], JSON_UNESCAPED_UNICODE); |
|
|
$logData['params'] = json_encode($logData['params'] ?? [], JSON_UNESCAPED_UNICODE); |
|
|
$logData['response'] = json_encode($logData['response'] ?? [], JSON_UNESCAPED_UNICODE); |
|
|
$logData['response'] = json_encode($logData['response'] ?? [], JSON_UNESCAPED_UNICODE); |
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
ApiLogModel::create($logData); |
|
|
ApiLogModel::create($logData); |
|
|
|
|
|
} catch (\Throwable $e) { |
|
|
|
|
|
// 如果是 MySQL 2006 错误,则重连后再执行 |
|
|
|
|
|
if (strpos($e->getMessage(), '2006 MySQL server has gone away') !== false) { |
|
|
|
|
|
Log::warning('MySQL connection lost, reconnecting...'); |
|
|
|
|
|
Db::disconnect(); // 清理旧连接 |
|
|
|
|
|
Db::reconnect(); // 重新连接 |
|
|
|
|
|
ApiLogModel::create($logData); // 重试一次 |
|
|
|
|
|
} else { |
|
|
|
|
|
throw $e; |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} else { |
|
|
} else { |
|
|
// 如果没有数据,休眠一段时间再继续 |
|
|
sleep(10); // 没数据时休眠 |
|
|
sleep(10); |
|
|
|
|
|
} |
|
|
} |
|
|
} catch (\Throwable $e) { |
|
|
} catch (\Throwable $e) { |
|
|
Log::error('ApiLogCommand error: ' . $e->getMessage()); |
|
|
Log::error('ApiLogCommand error: ' . $e->getMessage()); |
|
|