请求结果命中缓存, 记录响应日志
This commit is contained in:
parent
78f6b61d65
commit
9bd3030cb8
@ -106,11 +106,11 @@ func NewHttpClient(reqConfig *define.Request, cacheInstance cache.ICache) (*Http
|
|||||||
hc.OnRequestFinish(func(req *define.Request, rep *define.Response) {
|
hc.OnRequestFinish(func(req *define.Request, rep *define.Response) {
|
||||||
if rep.IsSuccess {
|
if rep.IsSuccess {
|
||||||
// 请求成功
|
// 请求成功
|
||||||
log.Record(req.Ctx, req.Logger, consts.LogLevelInfo, "接口请求成功", req, rep)
|
log.Record(consts.LogLevelInfo, "接口请求成功", req, rep)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// 请求失败
|
// 请求失败
|
||||||
log.Record(req.Ctx, req.Logger, consts.LogLevelError, "接口请求失败", req, rep)
|
log.Record(consts.LogLevelError, "接口请求失败", req, rep)
|
||||||
})
|
})
|
||||||
return hc, nil
|
return hc, nil
|
||||||
}
|
}
|
||||||
@ -292,6 +292,8 @@ func (hc *HttpClient) Request() *define.Response {
|
|||||||
_ = hc.requestBackendApi()
|
_ = hc.requestBackendApi()
|
||||||
}()
|
}()
|
||||||
}()
|
}()
|
||||||
|
// 命中缓存必然请求成功, 直接记录成功日志即可
|
||||||
|
log.Record(consts.LogLevelInfo, "接口请求成功:命中缓存", hc.reqConfig, cacheResult)
|
||||||
return cacheResult
|
return cacheResult
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -58,8 +58,8 @@ func GetBuildDataFunc() BuildHttpLogDataFunc {
|
|||||||
// Author : go_developer@163.com<白茶清欢>
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
//
|
//
|
||||||
// Date : 18:07 2025/3/31
|
// Date : 18:07 2025/3/31
|
||||||
func Record(ctx context.Context, logInstance *zap.Logger, level consts.LogLevel, msg string, reqCfg *define.Request, response *define.Response) {
|
func Record(level consts.LogLevel, msg string, reqCfg *define.Request, response *define.Response) {
|
||||||
if nil == logInstance {
|
if nil == reqCfg.Logger {
|
||||||
// 未设置日志实例
|
// 未设置日志实例
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -68,7 +68,8 @@ func Record(ctx context.Context, logInstance *zap.Logger, level consts.LogLevel,
|
|||||||
// 未设置构建日志数据的方法
|
// 未设置构建日志数据的方法
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
fields := buildDataFunc(ctx, true, nil, reqCfg, response)
|
fields := buildDataFunc(reqCfg.Ctx, true, nil, reqCfg, response)
|
||||||
|
logInstance := reqCfg.Logger
|
||||||
switch level {
|
switch level {
|
||||||
case consts.LogLevelDebug:
|
case consts.LogLevelDebug:
|
||||||
logInstance.Debug(msg, fields...)
|
logInstance.Debug(msg, fields...)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user