You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
57 lines
1.6 KiB
57 lines
1.6 KiB
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace app\command;
|
|
|
|
use app\model\ApiLogModel;
|
|
use think\console\Command;
|
|
use think\console\Input;
|
|
use think\console\Output;
|
|
use think\facade\Cache;
|
|
use think\facade\Log;
|
|
|
|
class ApiLogCommand extends Command
|
|
{
|
|
protected function configure()
|
|
{
|
|
// 指令配置
|
|
$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)
|
|
{
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|