From 311f896f0f972a04e3703a4580865b124edaf7b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Mon, 31 Mar 2025 18:31:17 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E9=85=8D=E7=BD=AE=E6=97=A5?= =?UTF-8?q?=E5=BF=97=E6=95=B0=E6=8D=AE=E7=94=9F=E6=88=90=E7=9A=84=E6=96=B9?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- httpclient/log/log.go | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/httpclient/log/log.go b/httpclient/log/log.go index 36894f2..0029787 100644 --- a/httpclient/log/log.go +++ b/httpclient/log/log.go @@ -1,4 +1,4 @@ -// Package httpclient ... +// Package log ... // // Description : httpclient ... // @@ -8,7 +8,9 @@ package log import ( + "context" "git.zhangdeman.cn/zhangdeman/consts" + "git.zhangdeman.cn/zhangdeman/network/httpclient/define" "go.uber.org/zap" ) @@ -34,16 +36,35 @@ func Get() *zap.Logger { return logger } +var buildHttpLogDataFunc BuildHttpLogDataFunc + +// BuildHttpLogDataFunc 构建http请求日志数据的方法 +type BuildHttpLogDataFunc func(ctx context.Context, reqCfg *define.Request, response *define.Response) []zap.Field + +func SetBuildDataFunc(f BuildHttpLogDataFunc) { + buildHttpLogDataFunc = f +} + +func GetBuildDataFunc() BuildHttpLogDataFunc { + return buildHttpLogDataFunc +} + // Record 日志记录 // // Author : go_developer@163.com<白茶清欢> // // Date : 18:07 2025/3/31 -func Record(logInstance *zap.Logger, level consts.LogLevel, msg string, fields ...zap.Field) { +func Record(ctx context.Context, logInstance *zap.Logger, level consts.LogLevel, msg string, reqCfg *define.Request, response *define.Response) { if nil == logInstance { // 未设置日志实例 return } + buildDataFunc := GetBuildDataFunc() + if nil == buildDataFunc { + // 未设置构建日志数据的方法 + return + } + fields := buildDataFunc(ctx, reqCfg, response) switch level { case consts.LogLevelDebug: logInstance.Debug(msg, fields...) @@ -56,5 +77,4 @@ func Record(logInstance *zap.Logger, level consts.LogLevel, msg string, fields . case consts.LogLevelPanic: logInstance.Panic(msg, fields...) } - }