p2 project
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.

51 lines
1.3 KiB

2 months ago
<?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()
{
1 month ago
$this->setName('api_log')->setDescription('the api_log command');
2 months ago
}
1 month ago
2 months ago
protected function execute(Input $input, Output $output)
{
4 weeks ago
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)) {
return;
2 months ago
}
4 weeks ago
$logData['params'] = json_encode($logData['params'] ?? [], JSON_UNESCAPED_UNICODE);
$logData['response'] = json_encode($logData['response'] ?? [], JSON_UNESCAPED_UNICODE);
try {
ApiLogModel::create($logData);
} catch (\Throwable $e) {
$redis->lpush('api_log', $result[1]);
throw $e;
}
} else {
return;
2 months ago
}
4 weeks ago
} catch (\Throwable $e) {
Log::error('ApiLogCommand error: ' . $e->getMessage());
2 months ago
}
4 weeks ago
return;
2 months ago
}
}