|
@@ -23,6 +23,66 @@ class AiMeasurement extends Api
|
|
|
'connect_timeout' => 30
|
|
|
];
|
|
|
|
|
|
+ // AI测量日志配置
|
|
|
+ private $logConfig = null;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取AI测量日志配置
|
|
|
+ */
|
|
|
+ private function getLogConfig()
|
|
|
+ {
|
|
|
+ if ($this->logConfig === null) {
|
|
|
+ $log_path = RUNTIME_PATH . 'log/ai_measurement/';
|
|
|
+ if (!is_dir($log_path)) {
|
|
|
+ @mkdir($log_path, 0755, true);
|
|
|
+ }
|
|
|
+
|
|
|
+ $this->logConfig = [
|
|
|
+ 'logger' => [
|
|
|
+ 'enable' => true,
|
|
|
+ 'file' => $log_path . 'ai_measurement.log',
|
|
|
+ 'level' => config('app_debug') ? 'debug' : 'info',
|
|
|
+ 'type' => 'daily',
|
|
|
+ 'max_file' => 30,
|
|
|
+ ]
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ return $this->logConfig;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 写入AI测量日志
|
|
|
+ * @param string $level 日志级别: info, error, debug, warning
|
|
|
+ * @param string $message 日志消息
|
|
|
+ * @param array $context 上下文数据
|
|
|
+ */
|
|
|
+ private function writeLog($level, $message, $context = [])
|
|
|
+ {
|
|
|
+ $config = $this->getLogConfig();
|
|
|
+ $log_path = dirname($config['logger']['file']);
|
|
|
+ $log_file = $config['logger']['file'];
|
|
|
+
|
|
|
+ // 如果是daily类型,添加日期后缀
|
|
|
+ if ($config['logger']['type'] === 'daily') {
|
|
|
+ $log_file = $log_path . '/ai_measurement_' . date('Y-m-d') . '.log';
|
|
|
+ }
|
|
|
+
|
|
|
+ $log_content = [
|
|
|
+ 'timestamp' => date('Y-m-d H:i:s'),
|
|
|
+ 'level' => strtoupper($level),
|
|
|
+ 'message' => $message,
|
|
|
+ 'context' => $context
|
|
|
+ ];
|
|
|
+
|
|
|
+ $log_text = '[' . $log_content['timestamp'] . '] ' . $log_content['level'] . ': ' . $log_content['message'];
|
|
|
+ if (!empty($context)) {
|
|
|
+ $log_text .= ' ' . json_encode($context, JSON_UNESCAPED_UNICODE);
|
|
|
+ }
|
|
|
+ $log_text .= PHP_EOL;
|
|
|
+
|
|
|
+ file_put_contents($log_file, $log_text, FILE_APPEND | LOCK_EX);
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 开始AI身体测量分析
|
|
|
*/
|
|
@@ -614,12 +674,12 @@ class AiMeasurement extends Api
|
|
|
$logData = [
|
|
|
'url' => $apiUrl,
|
|
|
'height' => $heightValue . 'cm',
|
|
|
- 'image1_preview' => isset($requestData['image1']) ? substr($requestData['image1'], 0, 50) . '...' : null,
|
|
|
- 'image2_preview' => isset($requestData['image2']) ? substr($requestData['image2'], 0, 50) . '...' : null,
|
|
|
- 'image3_preview' => isset($requestData['image3']) ? substr($requestData['image3'], 0, 50) . '...' : null,
|
|
|
+ 'image1' => isset($requestData['image1']) ? substr($requestData['image1'], 0, 50) . '...' : null,
|
|
|
+ 'image2' => isset($requestData['image2']) ? substr($requestData['image2'], 0, 50) . '...' : null,
|
|
|
+ 'image3' => isset($requestData['image3']) ? substr($requestData['image3'], 0, 50) . '...' : null,
|
|
|
'request_data_size' => strlen(json_encode($requestData)) . ' bytes'
|
|
|
];
|
|
|
- \think\Log::info('Calling third party AI service: ' . json_encode($logData));
|
|
|
+ $this->writeLog('info', 'Calling third party AI service', $logData);
|
|
|
|
|
|
// 发送POST请求
|
|
|
$ch = curl_init();
|
|
@@ -647,7 +707,11 @@ class AiMeasurement extends Api
|
|
|
throw new \Exception('请求第三方AI服务失败: ' . $error);
|
|
|
}
|
|
|
|
|
|
- \think\Log::info('Third party AI service response: ' .$response);
|
|
|
+ $this->writeLog('info', 'Third party AI service response received', [
|
|
|
+ 'response_size' => strlen($response) . ' bytes',
|
|
|
+ 'http_code' => $httpCode,
|
|
|
+ 'response_preview' => substr($response, 0, 200) . '...'
|
|
|
+ ]);
|
|
|
|
|
|
// 检查响应内容
|
|
|
if (empty($response)) {
|