优化日志数据生成
This commit is contained in:
		
							
								
								
									
										31
									
								
								define.go
									
									
									
									
									
								
							
							
						
						
									
										31
									
								
								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<白茶清欢> | ||||
|  | ||||
							
								
								
									
										41
									
								
								util.go
									
									
									
									
									
								
							
							
						
						
									
										41
									
								
								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 | ||||
| } | ||||
|  | ||||
|  | ||||
| @ -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...) | ||||
|  | ||||
		Reference in New Issue
	
	Block a user