From 80b1484ef0df0b8f79adcb3a7ba3f0cdd4fba568 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Wed, 24 Jul 2024 22:43:09 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=97=A5=E5=BF=97=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E7=94=9F=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- define.go | 31 ------------------------------- util.go | 41 ++++++++++++++++++++++++++++------------- wrapper/gorm_v2.go | 2 +- 3 files changed, 29 insertions(+), 45 deletions(-) diff --git a/define.go b/define.go index 91774bb..1c05cad 100644 --- a/define.go +++ b/define.go @@ -18,16 +18,8 @@ import ( "go.uber.org/zap/zapcore" ) -// FillLogDataFunc 定义填充数据的方法 -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 21:33 2024/7/24 -type FillLogDataFunc func(data *LogData) - var ( wsLoggerConnect storage.IConnection // ws 日志连接管理实例 - fillLogDataFunc FillLogDataFunc // 填充数据方法 ) // SetWsLoggConnect 设置ws connect @@ -48,29 +40,6 @@ func GetWsLoggConnect(connect storage.IConnection) { wsLoggerConnect = connect } -// SetFillLogDataFunc ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 21:34 2024/7/24 -func SetFillLogDataFunc(fillFunc FillLogDataFunc) { - fillLogDataFunc = fillFunc -} - -// GetFillLogDataFunc 获取填充数据的方法 -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 21:36 2024/7/24 -func GetFillLogDataFunc() FillLogDataFunc { - if nil == fillLogDataFunc { - return func(data *LogData) { - - } - } - return fillLogDataFunc -} - // LogData 记录日志数据 // // Author : go_developer@163.com<白茶清欢> diff --git a/util.go b/util.go index deaa0a5..918a864 100644 --- a/util.go +++ b/util.go @@ -8,39 +8,54 @@ package logger import ( + "context" + "fmt" "os" + "github.com/gin-gonic/gin" + + ginRequest "git.zhangdeman.cn/zhangdeman/gin/request" "git.zhangdeman.cn/zhangdeman/network/util" "git.zhangdeman.cn/zhangdeman/serialize" "go.uber.org/zap" ) +func getStrVal(ctx context.Context, key string) string { + if nil == ctx { + return "" + } + if ginCtx, ok := ctx.(*gin.Context); ok { + return ginRequest.WrapperHandle.GetCtxStringData(ginCtx, key, "") + } + val := ctx.Value(key) + if nil == val { + return "" + } + return fmt.Sprintf("%v", val) +} + // NewLogData ... // // Author : go_developer@163.com<白茶清欢> // // Date : 16:21 2024/7/23 -func NewLogData(logType string, code string, logData map[string]any) *LogData { - if logData == nil { - logData = make(map[string]any) - } +func NewLogData(ctx context.Context, logType string, code string, logData map[string]any) *LogData { hostname, _ := os.Hostname() commonLogData := &LogData{ - Uri: "", - TraceID: "", - UserID: "", - UserRoleID: "", - OperateMode: "", + Uri: getStrVal(ctx, "uri"), + TraceID: getStrVal(ctx, "trace_id"), + UserID: getStrVal(ctx, "user_id"), + UserRoleID: getStrVal(ctx, "user_role_id"), + OperateMode: getStrVal(ctx, "operate_mode"), LogType: logType, - CodeVersion: "", - ServiceVersion: "", - ClientIp: "", + CodeVersion: getStrVal(ctx, "code_version"), + ServiceVersion: getStrVal(ctx, "service_version"), + ClientIp: getStrVal(ctx, "client_ip"), ServerIp: util.IP.GetHostIP(), Hostname: hostname, Code: code, Data: logData, } - GetFillLogDataFunc()(commonLogData) return commonLogData } diff --git a/wrapper/gorm_v2.go b/wrapper/gorm_v2.go index 9a74a36..72804e3 100644 --- a/wrapper/gorm_v2.go +++ b/wrapper/gorm_v2.go @@ -191,7 +191,7 @@ func (g *Gorm) write(ctx context.Context, message string, level string, data map } } - dataList := logger2.ZapLogDataList(logger2.NewLogData(consts.LogTypeDatabase, "", data)) + dataList := logger2.ZapLogDataList(logger2.NewLogData(g.outCtx, consts.LogTypeDatabase, "", data)) switch strings.ToUpper(level) { case consts.LogLevelDebug: g.instance.Debug(message, dataList...)