fix ivyz7c9d
fix external_logger
This commit is contained in:
@@ -22,19 +22,9 @@ func ProcessIVYZ7C9DRequest(ctx context.Context, params []byte, deps *processors
|
||||
return nil, fmt.Errorf("%s: %w", processors.ErrInvalidParam, err)
|
||||
}
|
||||
|
||||
encryptedName, err := deps.ZhichaService.Encrypt(paramsDto.Name)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%s: %w", processors.ErrSystem, err)
|
||||
}
|
||||
|
||||
encryptedIDCard, err := deps.ZhichaService.Encrypt(paramsDto.IDCard)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%s: %w", processors.ErrSystem, err)
|
||||
}
|
||||
|
||||
reqData := map[string]interface{}{
|
||||
"name": encryptedName,
|
||||
"idCard": encryptedIDCard,
|
||||
"name": paramsDto.Name,
|
||||
"idCard": paramsDto.IDCard,
|
||||
"returnURL": paramsDto.ReturnURL,
|
||||
"orderId": paramsDto.UniqueID,
|
||||
}
|
||||
|
||||
@@ -71,19 +71,6 @@ func (w *WestDexService) generateRequestID() string {
|
||||
return fmt.Sprintf("westdex_%x", hash[:8])
|
||||
}
|
||||
|
||||
// buildLogData 构建包含transactionId的日志数据
|
||||
func (w *WestDexService) 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
|
||||
}
|
||||
|
||||
// buildRequestURL 构建请求URL
|
||||
func (w *WestDexService) buildRequestURL(code string) string {
|
||||
@@ -95,7 +82,7 @@ func (w *WestDexService) buildRequestURL(code string) string {
|
||||
func (w *WestDexService) CallAPI(ctx context.Context, code string, reqData map[string]interface{}) (resp []byte, err error) {
|
||||
startTime := time.Now()
|
||||
requestID := w.generateRequestID()
|
||||
|
||||
|
||||
// 从ctx中获取transactionId
|
||||
var transactionID string
|
||||
if ctxTransactionID, ok := ctx.Value("transaction_id").(string); ok {
|
||||
@@ -107,14 +94,14 @@ func (w *WestDexService) CallAPI(ctx context.Context, code string, reqData map[s
|
||||
|
||||
// 记录请求日志
|
||||
if w.logger != nil {
|
||||
w.logger.LogRequest(requestID, code, reqUrl, w.buildLogData(reqData, transactionID))
|
||||
w.logger.LogRequest(requestID, transactionID, code, reqUrl, reqData)
|
||||
}
|
||||
|
||||
jsonData, marshalErr := json.Marshal(reqData)
|
||||
if marshalErr != nil {
|
||||
err = fmt.Errorf("%w: %s", ErrSystem, marshalErr.Error())
|
||||
if w.logger != nil {
|
||||
w.logger.LogError(requestID, code, err, w.buildLogData(reqData, transactionID))
|
||||
w.logger.LogError(requestID, transactionID, code, err, reqData)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
@@ -124,7 +111,7 @@ func (w *WestDexService) CallAPI(ctx context.Context, code string, reqData map[s
|
||||
if newRequestErr != nil {
|
||||
err = fmt.Errorf("%w: %s", ErrSystem, newRequestErr.Error())
|
||||
if w.logger != nil {
|
||||
w.logger.LogError(requestID, code, err, w.buildLogData(reqData, transactionID))
|
||||
w.logger.LogError(requestID, transactionID, code, err, reqData)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
@@ -138,7 +125,7 @@ func (w *WestDexService) CallAPI(ctx context.Context, code string, reqData map[s
|
||||
if clientDoErr != nil {
|
||||
err = fmt.Errorf("%w: %s", ErrSystem, clientDoErr.Error())
|
||||
if w.logger != nil {
|
||||
w.logger.LogError(requestID, code, err, w.buildLogData(reqData, transactionID))
|
||||
w.logger.LogError(requestID, transactionID, code, err, reqData)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
@@ -147,7 +134,7 @@ func (w *WestDexService) CallAPI(ctx context.Context, code string, reqData map[s
|
||||
if closeErr != nil {
|
||||
// 记录关闭错误
|
||||
if w.logger != nil {
|
||||
w.logger.LogError(requestID, code, fmt.Errorf("关闭响应体失败: %w", closeErr), w.buildLogData(reqData, transactionID))
|
||||
w.logger.LogError(requestID, transactionID, code, fmt.Errorf("关闭响应体失败: %w", closeErr), reqData)
|
||||
}
|
||||
}
|
||||
}(httpResp.Body)
|
||||
@@ -162,32 +149,32 @@ func (w *WestDexService) CallAPI(ctx context.Context, code string, reqData map[s
|
||||
if ReadErr != nil {
|
||||
err = fmt.Errorf("%w: %s", ErrSystem, ReadErr.Error())
|
||||
if w.logger != nil {
|
||||
w.logger.LogError(requestID, code, err, w.buildLogData(reqData, transactionID))
|
||||
w.logger.LogError(requestID, transactionID, code, err, reqData)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 记录响应日志
|
||||
if w.logger != nil {
|
||||
w.logger.LogResponse(requestID, code, httpResp.StatusCode, bodyBytes, duration)
|
||||
}
|
||||
|
||||
// 手动调用 json.Unmarshal 触发自定义的 UnmarshalJSON 方法
|
||||
var westDexResp WestResp
|
||||
UnmarshalErr := json.Unmarshal(bodyBytes, &westDexResp)
|
||||
if UnmarshalErr != nil {
|
||||
err = UnmarshalErr
|
||||
if w.logger != nil {
|
||||
w.logger.LogError(requestID, code, err, w.buildLogData(reqData, transactionID))
|
||||
w.logger.LogError(requestID, transactionID, code, err, reqData)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 记录响应日志,包含响应ID
|
||||
if w.logger != nil {
|
||||
w.logger.LogResponseWithID(requestID, transactionID, code, httpResp.StatusCode, bodyBytes, duration, westDexResp.ID)
|
||||
}
|
||||
|
||||
if westDexResp.Code != "00000" && westDexResp.Code != "200" && westDexResp.Code != "0" {
|
||||
if westDexResp.Data == "" {
|
||||
err = fmt.Errorf("%w: %s", ErrSystem, westDexResp.Message)
|
||||
if w.logger != nil {
|
||||
w.logger.LogError(requestID, code, err, w.buildLogData(reqData, transactionID))
|
||||
w.logger.LogErrorWithResponseID(requestID, transactionID, code, err, reqData, westDexResp.ID)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
@@ -195,14 +182,14 @@ func (w *WestDexService) CallAPI(ctx context.Context, code string, reqData map[s
|
||||
if DecryptErr != nil {
|
||||
err = fmt.Errorf("%w: %s", ErrSystem, DecryptErr.Error())
|
||||
if w.logger != nil {
|
||||
w.logger.LogError(requestID, code, err, w.buildLogData(reqData, transactionID))
|
||||
w.logger.LogErrorWithResponseID(requestID, transactionID, code, err, reqData, westDexResp.ID)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 记录业务错误日志
|
||||
// 记录业务错误日志,包含响应ID
|
||||
if w.logger != nil {
|
||||
w.logger.LogError(requestID, code, fmt.Errorf("%w: %s", ErrDatasource, westDexResp.Message), w.buildLogData(reqData, transactionID))
|
||||
w.logger.LogErrorWithResponseID(requestID, transactionID, code, fmt.Errorf("%w: %s", ErrDatasource, westDexResp.Message), reqData, westDexResp.ID)
|
||||
}
|
||||
|
||||
// 记录性能日志(失败)
|
||||
@@ -214,7 +201,7 @@ func (w *WestDexService) CallAPI(ctx context.Context, code string, reqData map[s
|
||||
if westDexResp.Data == "" {
|
||||
err = fmt.Errorf("%w: %s", ErrSystem, westDexResp.Message)
|
||||
if w.logger != nil {
|
||||
w.logger.LogError(requestID, code, err, w.buildLogData(reqData, transactionID))
|
||||
w.logger.LogErrorWithResponseID(requestID, transactionID, code, err, reqData, westDexResp.ID)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
@@ -223,7 +210,7 @@ func (w *WestDexService) CallAPI(ctx context.Context, code string, reqData map[s
|
||||
if DecryptErr != nil {
|
||||
err = fmt.Errorf("%w: %s", ErrSystem, DecryptErr.Error())
|
||||
if w.logger != nil {
|
||||
w.logger.LogError(requestID, code, err, w.buildLogData(reqData, transactionID))
|
||||
w.logger.LogErrorWithResponseID(requestID, transactionID, code, err, reqData, westDexResp.ID)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
@@ -237,7 +224,7 @@ func (w *WestDexService) CallAPI(ctx context.Context, code string, reqData map[s
|
||||
// 记录HTTP错误
|
||||
err = fmt.Errorf("%w: 西部请求失败Code: %d", ErrSystem, httpResp.StatusCode)
|
||||
if w.logger != nil {
|
||||
w.logger.LogError(requestID, code, err, w.buildLogData(reqData, transactionID))
|
||||
w.logger.LogError(requestID, transactionID, code, err, reqData)
|
||||
// 注意:通用日志系统不包含性能日志功能
|
||||
}
|
||||
|
||||
@@ -248,7 +235,7 @@ func (w *WestDexService) CallAPI(ctx context.Context, code string, reqData map[s
|
||||
func (w *WestDexService) G05HZ01CallAPI(ctx context.Context, code string, reqData map[string]interface{}) (resp []byte, err error) {
|
||||
startTime := time.Now()
|
||||
requestID := w.generateRequestID()
|
||||
|
||||
|
||||
// 从ctx中获取transactionId
|
||||
var transactionID string
|
||||
if ctxTransactionID, ok := ctx.Value("transaction_id").(string); ok {
|
||||
@@ -260,14 +247,14 @@ func (w *WestDexService) G05HZ01CallAPI(ctx context.Context, code string, reqDat
|
||||
|
||||
// 记录请求日志
|
||||
if w.logger != nil {
|
||||
w.logger.LogRequest(requestID, code, reqUrl, w.buildLogData(reqData, transactionID))
|
||||
w.logger.LogRequest(requestID, transactionID, code, reqUrl, reqData)
|
||||
}
|
||||
|
||||
jsonData, marshalErr := json.Marshal(reqData)
|
||||
if marshalErr != nil {
|
||||
err = fmt.Errorf("%w: %s", ErrSystem, marshalErr.Error())
|
||||
if w.logger != nil {
|
||||
w.logger.LogError(requestID, code, err, w.buildLogData(reqData, transactionID))
|
||||
w.logger.LogError(requestID, transactionID, code, err, reqData)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
@@ -277,7 +264,7 @@ func (w *WestDexService) G05HZ01CallAPI(ctx context.Context, code string, reqDat
|
||||
if newRequestErr != nil {
|
||||
err = fmt.Errorf("%w: %s", ErrSystem, newRequestErr.Error())
|
||||
if w.logger != nil {
|
||||
w.logger.LogError(requestID, code, err, w.buildLogData(reqData, transactionID))
|
||||
w.logger.LogError(requestID, transactionID, code, err, reqData)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
@@ -291,7 +278,7 @@ func (w *WestDexService) G05HZ01CallAPI(ctx context.Context, code string, reqDat
|
||||
if clientDoErr != nil {
|
||||
err = fmt.Errorf("%w: %s", ErrSystem, clientDoErr.Error())
|
||||
if w.logger != nil {
|
||||
w.logger.LogError(requestID, code, err, w.buildLogData(reqData, transactionID))
|
||||
w.logger.LogError(requestID, transactionID, code, err, reqData)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
@@ -300,7 +287,7 @@ func (w *WestDexService) G05HZ01CallAPI(ctx context.Context, code string, reqDat
|
||||
if closeErr != nil {
|
||||
// 记录关闭错误
|
||||
if w.logger != nil {
|
||||
w.logger.LogError(requestID, code, fmt.Errorf("关闭响应体失败: %w", closeErr), w.buildLogData(reqData, transactionID))
|
||||
w.logger.LogError(requestID, transactionID, code, fmt.Errorf("关闭响应体失败: %w", closeErr), reqData)
|
||||
}
|
||||
}
|
||||
}(httpResp.Body)
|
||||
@@ -313,37 +300,37 @@ func (w *WestDexService) G05HZ01CallAPI(ctx context.Context, code string, reqDat
|
||||
if ReadErr != nil {
|
||||
err = fmt.Errorf("%w: %s", ErrSystem, ReadErr.Error())
|
||||
if w.logger != nil {
|
||||
w.logger.LogError(requestID, code, err, w.buildLogData(reqData, transactionID))
|
||||
w.logger.LogError(requestID, transactionID, code, err, reqData)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 记录响应日志
|
||||
if w.logger != nil {
|
||||
w.logger.LogResponse(requestID, code, httpResp.StatusCode, bodyBytes, duration)
|
||||
}
|
||||
|
||||
var westDexResp G05HZ01WestResp
|
||||
UnmarshalErr := json.Unmarshal(bodyBytes, &westDexResp)
|
||||
if UnmarshalErr != nil {
|
||||
err = fmt.Errorf("%w: %s", ErrSystem, UnmarshalErr.Error())
|
||||
if w.logger != nil {
|
||||
w.logger.LogError(requestID, code, err, w.buildLogData(reqData, transactionID))
|
||||
w.logger.LogError(requestID, transactionID, code, err, reqData)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 记录响应日志,包含响应ID
|
||||
if w.logger != nil {
|
||||
w.logger.LogResponseWithID(requestID, transactionID, code, httpResp.StatusCode, bodyBytes, duration, westDexResp.ID)
|
||||
}
|
||||
|
||||
if westDexResp.Code != "0000" {
|
||||
if westDexResp.Data == nil {
|
||||
err = fmt.Errorf("%w: %s", ErrSystem, westDexResp.Message)
|
||||
if w.logger != nil {
|
||||
w.logger.LogError(requestID, code, err, w.buildLogData(reqData, transactionID))
|
||||
w.logger.LogErrorWithResponseID(requestID, transactionID, code, err, reqData, westDexResp.ID)
|
||||
}
|
||||
return nil, err
|
||||
} else {
|
||||
// 记录业务错误日志
|
||||
// 记录业务错误日志,包含响应ID
|
||||
if w.logger != nil {
|
||||
w.logger.LogError(requestID, code, fmt.Errorf("%w: %s", ErrSystem, string(westDexResp.Data)), w.buildLogData(reqData, transactionID))
|
||||
w.logger.LogErrorWithResponseID(requestID, transactionID, code, fmt.Errorf("%w: %s", ErrSystem, string(westDexResp.Data)), reqData, westDexResp.ID)
|
||||
}
|
||||
|
||||
// 记录性能日志(失败)
|
||||
@@ -356,7 +343,7 @@ func (w *WestDexService) G05HZ01CallAPI(ctx context.Context, code string, reqDat
|
||||
if westDexResp.Data == nil {
|
||||
err = fmt.Errorf("%w: %s", ErrSystem, westDexResp.Message)
|
||||
if w.logger != nil {
|
||||
w.logger.LogError(requestID, code, err, w.buildLogData(reqData, transactionID))
|
||||
w.logger.LogErrorWithResponseID(requestID, transactionID, code, err, reqData, westDexResp.ID)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
@@ -369,7 +356,7 @@ func (w *WestDexService) G05HZ01CallAPI(ctx context.Context, code string, reqDat
|
||||
// 记录HTTP错误
|
||||
err = fmt.Errorf("%w: 西部请求失败Code: %d", ErrSystem, httpResp.StatusCode)
|
||||
if w.logger != nil {
|
||||
w.logger.LogError(requestID, code, err, w.buildLogData(reqData, transactionID))
|
||||
w.logger.LogError(requestID, transactionID, code, err, reqData)
|
||||
// 注意:通用日志系统不包含性能日志功能
|
||||
}
|
||||
return nil, err
|
||||
|
||||
@@ -64,7 +64,7 @@ func (y *YushanService) CallAPI(ctx context.Context, code string, params map[str
|
||||
|
||||
// 记录请求日志
|
||||
if y.logger != nil {
|
||||
y.logger.LogRequest(requestID, code, y.config.URL, y.buildLogData(params, transactionID))
|
||||
y.logger.LogRequest(requestID, transactionID, code, y.config.URL, params)
|
||||
}
|
||||
|
||||
// 获取当前时间戳
|
||||
@@ -86,7 +86,7 @@ func (y *YushanService) CallAPI(ctx context.Context, code string, params map[str
|
||||
if err != nil {
|
||||
err = fmt.Errorf("%w: %s", ErrSystem, err.Error())
|
||||
if y.logger != nil {
|
||||
y.logger.LogError(requestID, code, err, y.buildLogData(params, transactionID))
|
||||
y.logger.LogError(requestID, transactionID, code, err, params)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
@@ -96,7 +96,7 @@ func (y *YushanService) CallAPI(ctx context.Context, code string, params map[str
|
||||
if err != nil {
|
||||
err = fmt.Errorf("%w: %s", ErrSystem, err.Error())
|
||||
if y.logger != nil {
|
||||
y.logger.LogError(requestID, code, err, y.buildLogData(params, transactionID))
|
||||
y.logger.LogError(requestID, transactionID, code, err, params)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
@@ -115,7 +115,7 @@ func (y *YushanService) CallAPI(ctx context.Context, code string, params map[str
|
||||
if err != nil {
|
||||
err = fmt.Errorf("%w: %s", ErrSystem, err.Error())
|
||||
if y.logger != nil {
|
||||
y.logger.LogError(requestID, code, err, y.buildLogData(params, transactionID))
|
||||
y.logger.LogError(requestID, transactionID, code, err, params)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
@@ -127,7 +127,7 @@ func (y *YushanService) CallAPI(ctx context.Context, code string, params map[str
|
||||
if err != nil {
|
||||
err = fmt.Errorf("%w: %s", ErrSystem, err.Error())
|
||||
if y.logger != nil {
|
||||
y.logger.LogError(requestID, code, err, y.buildLogData(params, transactionID))
|
||||
y.logger.LogError(requestID, transactionID, code, err, params)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
@@ -137,7 +137,7 @@ func (y *YushanService) CallAPI(ctx context.Context, code string, params map[str
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
if y.logger != nil {
|
||||
y.logger.LogError(requestID, code, err, y.buildLogData(params, transactionID))
|
||||
y.logger.LogError(requestID, transactionID, code, err, params)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
@@ -151,7 +151,7 @@ func (y *YushanService) CallAPI(ctx context.Context, code string, params map[str
|
||||
if err != nil {
|
||||
err = fmt.Errorf("%w: %s", ErrSystem, err.Error())
|
||||
if y.logger != nil {
|
||||
y.logger.LogError(requestID, code, err, y.buildLogData(params, transactionID))
|
||||
y.logger.LogError(requestID, transactionID, code, err, params)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
@@ -162,7 +162,7 @@ func (y *YushanService) CallAPI(ctx context.Context, code string, params map[str
|
||||
// 记录响应日志
|
||||
if y.logger != nil {
|
||||
duration := time.Since(startTime)
|
||||
y.logger.LogResponse(requestID, code, resp.StatusCode, respData, duration)
|
||||
y.logger.LogResponse(requestID, transactionID, code, resp.StatusCode, respData, duration)
|
||||
}
|
||||
|
||||
if retCode == "100000" {
|
||||
@@ -174,7 +174,7 @@ func (y *YushanService) CallAPI(ctx context.Context, code string, params map[str
|
||||
if !retData.Exists() {
|
||||
err = fmt.Errorf("%w: %s", ErrDatasource, "羽山请求retdata为空")
|
||||
if y.logger != nil {
|
||||
y.logger.LogError(requestID, code, err, y.buildLogData(params, transactionID))
|
||||
y.logger.LogError(requestID, transactionID, code, err, params)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
@@ -182,7 +182,7 @@ func (y *YushanService) CallAPI(ctx context.Context, code string, params map[str
|
||||
} else {
|
||||
err = fmt.Errorf("%w: %s", ErrDatasource, "羽山请求未知的状态码")
|
||||
if y.logger != nil {
|
||||
y.logger.LogError(requestID, code, err, y.buildLogData(params, transactionID))
|
||||
y.logger.LogError(requestID, transactionID, code, err, params)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
@@ -195,20 +195,6 @@ func (y *YushanService) generateRequestID() string {
|
||||
return fmt.Sprintf("yushan_%x", hash[:8])
|
||||
}
|
||||
|
||||
// buildLogData 构建包含transactionId的日志数据
|
||||
func (y *YushanService) 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
|
||||
}
|
||||
|
||||
// GenerateRandomString 生成一个32位的随机字符串订单号
|
||||
func (y *YushanService) GenerateRandomString() (string, error) {
|
||||
// 创建一个16字节的数组
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
// 对外统一返回数据源异常错误
|
||||
|
||||
@@ -152,7 +152,7 @@ func createFileWriter(logDir, level string, config ExternalServiceLevelFileConfi
|
||||
// 按天分隔:logs/westdex/2024-01-01/westdex_info.log
|
||||
date := time.Now().Format("2006-01-02")
|
||||
dateDir := filepath.Join(logDir, date)
|
||||
|
||||
|
||||
// 确保日期目录存在
|
||||
if err := os.MkdirAll(dateDir, 0755); err != nil {
|
||||
// 如果创建日期目录失败,回退到根目录
|
||||
@@ -175,10 +175,11 @@ func createFileWriter(logDir, level string, config ExternalServiceLevelFileConfi
|
||||
}
|
||||
|
||||
// LogRequest 记录请求日志
|
||||
func (e *ExternalServiceLogger) LogRequest(requestID, apiCode string, url interface{}, params interface{}) {
|
||||
func (e *ExternalServiceLogger) LogRequest(requestID, transactionID, apiCode string, url interface{}, params interface{}) {
|
||||
e.logger.Info(fmt.Sprintf("%s API请求", e.serviceName),
|
||||
zap.String("service", e.serviceName),
|
||||
zap.String("request_id", requestID),
|
||||
zap.String("transaction_id", transactionID),
|
||||
zap.String("api_code", apiCode),
|
||||
zap.Any("url", url),
|
||||
zap.Any("params", params),
|
||||
@@ -187,10 +188,11 @@ func (e *ExternalServiceLogger) LogRequest(requestID, apiCode string, url interf
|
||||
}
|
||||
|
||||
// LogResponse 记录响应日志
|
||||
func (e *ExternalServiceLogger) LogResponse(requestID, apiCode string, statusCode int, response []byte, duration time.Duration) {
|
||||
func (e *ExternalServiceLogger) LogResponse(requestID, transactionID, apiCode string, statusCode int, response []byte, duration time.Duration) {
|
||||
e.logger.Info(fmt.Sprintf("%s API响应", e.serviceName),
|
||||
zap.String("service", e.serviceName),
|
||||
zap.String("request_id", requestID),
|
||||
zap.String("transaction_id", transactionID),
|
||||
zap.String("api_code", apiCode),
|
||||
zap.Int("status_code", statusCode),
|
||||
zap.String("response", string(response)),
|
||||
@@ -199,11 +201,27 @@ func (e *ExternalServiceLogger) LogResponse(requestID, apiCode string, statusCod
|
||||
)
|
||||
}
|
||||
|
||||
// LogResponseWithID 记录包含响应ID的响应日志
|
||||
func (e *ExternalServiceLogger) LogResponseWithID(requestID, transactionID, apiCode string, statusCode int, response []byte, duration time.Duration, responseID string) {
|
||||
e.logger.Info(fmt.Sprintf("%s API响应", e.serviceName),
|
||||
zap.String("service", e.serviceName),
|
||||
zap.String("request_id", requestID),
|
||||
zap.String("transaction_id", transactionID),
|
||||
zap.String("api_code", apiCode),
|
||||
zap.Int("status_code", statusCode),
|
||||
zap.String("response", string(response)),
|
||||
zap.Duration("duration", duration),
|
||||
zap.String("response_id", responseID),
|
||||
zap.String("timestamp", time.Now().Format(time.RFC3339)),
|
||||
)
|
||||
}
|
||||
|
||||
// LogError 记录错误日志
|
||||
func (e *ExternalServiceLogger) LogError(requestID, apiCode string, err error, params interface{}) {
|
||||
func (e *ExternalServiceLogger) LogError(requestID, transactionID, apiCode string, err error, params interface{}) {
|
||||
e.logger.Error(fmt.Sprintf("%s API错误", e.serviceName),
|
||||
zap.String("service", e.serviceName),
|
||||
zap.String("request_id", requestID),
|
||||
zap.String("transaction_id", transactionID),
|
||||
zap.String("api_code", apiCode),
|
||||
zap.Error(err),
|
||||
zap.Any("params", params),
|
||||
@@ -211,6 +229,20 @@ func (e *ExternalServiceLogger) LogError(requestID, apiCode string, err error, p
|
||||
)
|
||||
}
|
||||
|
||||
// LogErrorWithResponseID 记录包含响应ID的错误日志
|
||||
func (e *ExternalServiceLogger) LogErrorWithResponseID(requestID, transactionID, apiCode string, err error, params interface{}, responseID string) {
|
||||
e.logger.Error(fmt.Sprintf("%s API错误", e.serviceName),
|
||||
zap.String("service", e.serviceName),
|
||||
zap.String("request_id", requestID),
|
||||
zap.String("transaction_id", transactionID),
|
||||
zap.String("api_code", apiCode),
|
||||
zap.Error(err),
|
||||
zap.Any("params", params),
|
||||
zap.String("response_id", responseID),
|
||||
zap.String("timestamp", time.Now().Format(time.RFC3339)),
|
||||
)
|
||||
}
|
||||
|
||||
// LogInfo 记录信息日志
|
||||
func (e *ExternalServiceLogger) LogInfo(message string, fields ...zap.Field) {
|
||||
allFields := []zap.Field{zap.String("service", e.serviceName)}
|
||||
@@ -244,7 +276,7 @@ func (e *ExternalServiceLogger) CleanupOldDateDirs() error {
|
||||
}
|
||||
|
||||
logDir := filepath.Join(e.config.LogDir, e.serviceName)
|
||||
|
||||
|
||||
// 读取日志目录
|
||||
entries, err := os.ReadDir(logDir)
|
||||
if err != nil {
|
||||
@@ -256,7 +288,7 @@ func (e *ExternalServiceLogger) CleanupOldDateDirs() error {
|
||||
if errorConfig, exists := e.config.LevelConfigs["error"]; exists && errorConfig.MaxAge > 0 {
|
||||
maxAge = errorConfig.MaxAge
|
||||
}
|
||||
|
||||
|
||||
cutoffTime := time.Now().AddDate(0, 0, -maxAge)
|
||||
|
||||
for _, entry := range entries {
|
||||
|
||||
Reference in New Issue
Block a user