fix ivyz7c9d

fix external_logger
This commit is contained in:
2025-08-26 16:03:46 +08:00
parent 2a93d120f1
commit a91bde0c67
5 changed files with 97 additions and 116 deletions

View File

@@ -63,20 +63,6 @@ func (z *ZhichaService) generateRequestID() string {
return fmt.Sprintf("zhicha_%x", hash[:8])
}
// buildLogData 构建包含transactionId的日志数据
func (z *ZhichaService) buildLogData(data map[string]interface{}, transactionID string) map[string]interface{} {
if transactionID == "" {
return data
}
logData := data
if logData == nil {
logData = make(map[string]interface{})
}
logData["transaction_id"] = transactionID
return logData
}
// generateSign 生成签名
func (z *ZhichaService) generateSign(timestamp int64) string {
// 第一步对app_secret进行MD5加密
@@ -103,14 +89,14 @@ func (z *ZhichaService) CallAPI(ctx context.Context, proID string, params map[st
// 记录请求日志
if z.logger != nil {
z.logger.LogRequest(requestID, "handle", z.config.URL, z.buildLogData(params, transactionID))
z.logger.LogRequest(requestID, transactionID, proID, z.config.URL, params)
}
jsonData, marshalErr := json.Marshal(params)
if marshalErr != nil {
err = fmt.Errorf("%w: %s", ErrSystem, marshalErr.Error())
if z.logger != nil {
z.logger.LogError(requestID, "handle", err, z.buildLogData(params, transactionID))
z.logger.LogError(requestID, transactionID, proID, err, params)
}
return nil, err
}
@@ -120,7 +106,7 @@ func (z *ZhichaService) CallAPI(ctx context.Context, proID string, params map[st
if err != nil {
err = fmt.Errorf("%w: %s", ErrSystem, err.Error())
if z.logger != nil {
z.logger.LogError(requestID, "handle", err, z.buildLogData(params, transactionID))
z.logger.LogError(requestID, transactionID, proID, err, params)
}
return nil, err
}
@@ -142,7 +128,7 @@ func (z *ZhichaService) CallAPI(ctx context.Context, proID string, params map[st
if err != nil {
err = fmt.Errorf("%w: %s", ErrSystem, err.Error())
if z.logger != nil {
z.logger.LogError(requestID, "handle", err, z.buildLogData(params, transactionID))
z.logger.LogError(requestID, transactionID, proID, err, params)
}
return nil, err
}
@@ -153,7 +139,7 @@ func (z *ZhichaService) CallAPI(ctx context.Context, proID string, params map[st
if err != nil {
err = fmt.Errorf("%w: %s", ErrSystem, err.Error())
if z.logger != nil {
z.logger.LogError(requestID, "handle", err, z.buildLogData(params, transactionID))
z.logger.LogError(requestID, transactionID, proID, err, params)
}
return nil, err
}
@@ -161,14 +147,14 @@ func (z *ZhichaService) CallAPI(ctx context.Context, proID string, params map[st
// 记录响应日志
if z.logger != nil {
duration := time.Since(startTime)
z.logger.LogResponse(requestID, "handle", response.StatusCode, respBody, duration)
z.logger.LogResponse(requestID, transactionID, proID, response.StatusCode, respBody, duration)
}
// 检查HTTP状态码
if response.StatusCode != http.StatusOK {
err = fmt.Errorf("%w: HTTP状态码 %d", ErrDatasource, response.StatusCode)
if z.logger != nil {
z.logger.LogError(requestID, "handle", err, z.buildLogData(params, transactionID))
z.logger.LogError(requestID, transactionID, proID, err, params)
}
return nil, err
}
@@ -178,7 +164,7 @@ func (z *ZhichaService) CallAPI(ctx context.Context, proID string, params map[st
if err := json.Unmarshal(respBody, &zhichaResp); err != nil {
err = fmt.Errorf("%w: 响应解析失败: %s", ErrSystem, err.Error())
if z.logger != nil {
z.logger.LogError(requestID, "handle", err, z.buildLogData(params, transactionID))
z.logger.LogError(requestID, transactionID, proID, err, params)
}
return nil, err
}
@@ -193,7 +179,7 @@ func (z *ZhichaService) CallAPI(ctx context.Context, proID string, params map[st
// 记录智查金控的详细错误信息到日志
if z.logger != nil {
z.logger.LogError(requestID, "handle", zhichaErr, z.buildLogData(params, transactionID))
z.logger.LogError(requestID, transactionID, proID, zhichaErr, params)
}
// 对外统一返回数据源异常错误