升级数据库日志
This commit is contained in:
		
							
								
								
									
										6
									
								
								go.mod
									
									
									
									
									
								
							
							
						
						
									
										6
									
								
								go.mod
									
									
									
									
									
								
							| @ -5,8 +5,10 @@ go 1.21 | ||||
| toolchain go1.22.5 | ||||
|  | ||||
| require ( | ||||
| 	git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240723085016-ee0510753552 | ||||
| 	git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240724140052-d0784926366a | ||||
| 	git.zhangdeman.cn/zhangdeman/gin v0.0.0-20240723135300-86cecc4eb707 | ||||
| 	git.zhangdeman.cn/zhangdeman/network v0.0.0-20230925112156-f0eb86dd2442 | ||||
| 	git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20240618035451-8d48a6bd39dd | ||||
| 	git.zhangdeman.cn/zhangdeman/websocket v0.0.0-20240723075210-85feada512b2 | ||||
| 	github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible | ||||
| 	github.com/pkg/errors v0.9.1 | ||||
| @ -17,9 +19,7 @@ require ( | ||||
| require ( | ||||
| 	git.zhangdeman.cn/zhangdeman/easylock v0.0.0-20230731062340-983985c12eda // indirect | ||||
| 	git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20240311030808-e2a2e6a3c211 // indirect | ||||
| 	git.zhangdeman.cn/zhangdeman/network v0.0.0-20230925112156-f0eb86dd2442 // indirect | ||||
| 	git.zhangdeman.cn/zhangdeman/op_type v0.0.0-20240122104027-4928421213c0 // indirect | ||||
| 	git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20240618035451-8d48a6bd39dd // indirect | ||||
| 	git.zhangdeman.cn/zhangdeman/util v0.0.0-20240618042405-6ee2c904644e // indirect | ||||
| 	git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20240627031706-9ff1c213bb50 // indirect | ||||
| 	github.com/BurntSushi/toml v1.4.0 // indirect | ||||
|  | ||||
							
								
								
									
										2
									
								
								go.sum
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								go.sum
									
									
									
									
									
								
							| @ -2,6 +2,8 @@ git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240722080005-ca68a3ff8bc7 h1:8wJlcu | ||||
| git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240722080005-ca68a3ff8bc7/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k= | ||||
| git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240723085016-ee0510753552 h1:SgxcaLXEXn3ImMOik9y3xDz82KwK/+9IhqV1kZbqIwQ= | ||||
| git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240723085016-ee0510753552/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k= | ||||
| git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240724140052-d0784926366a h1:xeDsU7YV/6ae+/imMpAYjxFpmbCvoCmIHTN40UInreU= | ||||
| git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240724140052-d0784926366a/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k= | ||||
| git.zhangdeman.cn/zhangdeman/easylock v0.0.0-20230731062340-983985c12eda h1:bMD6r9gjRy7cO+T4zRQVYAesgIblBdTnhzT1vN5wjvI= | ||||
| git.zhangdeman.cn/zhangdeman/easylock v0.0.0-20230731062340-983985c12eda/go.mod h1:dT0rmHcJ9Z9IqWeMIt7YzR88nKkNV2V3dfG0j9Q6lK0= | ||||
| git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20240311030808-e2a2e6a3c211 h1:I/wOsRpCSRkU9vo1u703slQsmK0wnNeZzsWQOGtIAG0= | ||||
|  | ||||
							
								
								
									
										10
									
								
								util.go
									
									
									
									
									
								
							
							
						
						
									
										10
									
								
								util.go
									
									
									
									
									
								
							| @ -10,10 +10,8 @@ package logger | ||||
| import ( | ||||
| 	"os" | ||||
|  | ||||
| 	ginRequest "git.zhangdeman.cn/zhangdeman/gin/request" | ||||
| 	"git.zhangdeman.cn/zhangdeman/network/util" | ||||
| 	"git.zhangdeman.cn/zhangdeman/serialize" | ||||
| 	"github.com/gin-gonic/gin" | ||||
| 	"go.uber.org/zap" | ||||
| ) | ||||
|  | ||||
| @ -22,13 +20,13 @@ import ( | ||||
| // Author : go_developer@163.com<白茶清欢> | ||||
| // | ||||
| // Date : 16:21 2024/7/23 | ||||
| func NewLogData(ctx *gin.Context, logType string, code string, logData map[string]any) *LogData { | ||||
| func NewLogData(logType string, code string, logData map[string]any) *LogData { | ||||
| 	if logData == nil { | ||||
| 		logData = make(map[string]any) | ||||
| 	} | ||||
| 	hostname, _ := os.Hostname() | ||||
| 	commonLogData := &LogData{ | ||||
| 		Uri:            ginRequest.WrapperHandle.GetUri(ctx, ""), | ||||
| 		Uri:            "", | ||||
| 		TraceID:        "", | ||||
| 		UserID:         "", | ||||
| 		UserRoleID:     "", | ||||
| @ -36,7 +34,7 @@ func NewLogData(ctx *gin.Context, logType string, code string, logData map[strin | ||||
| 		LogType:        logType, | ||||
| 		CodeVersion:    "", | ||||
| 		ServiceVersion: "", | ||||
| 		ClientIp:       ginRequest.WrapperHandle.GetClientIp(ctx, "0.0.0.0"), | ||||
| 		ClientIp:       "", | ||||
| 		ServerIp:       util.IP.GetHostIP(), | ||||
| 		Hostname:       hostname, | ||||
| 		Code:           code, | ||||
| @ -51,7 +49,7 @@ func NewLogData(ctx *gin.Context, logType string, code string, logData map[strin | ||||
| // Author : go_developer@163.com<白茶清欢> | ||||
| // | ||||
| // Date : 17:33 2024/7/23 | ||||
| func ZapLogDataList(ctx *gin.Context, logData *LogData) []zap.Field { | ||||
| func ZapLogDataList(logData *LogData) []zap.Field { | ||||
| 	logDataList := make([]zap.Field, 0) | ||||
| 	if logData == nil { | ||||
| 		return logDataList | ||||
|  | ||||
| @ -102,7 +102,9 @@ func (g *Gorm) LogMode(level logger.LogLevel) logger.Interface { | ||||
| // | ||||
| // Date : 10:18 下午 2021/3/1 | ||||
| func (g *Gorm) Info(ctx context.Context, s string, i ...any) { | ||||
| 	g.write(ctx, s, consts.LogLevelInfo, []zap.Field{zap.Any("log_data", i)}) | ||||
| 	g.write(ctx, s, consts.LogLevelInfo, map[string]any{ | ||||
| 		"log_data": i, | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| // Warn ... | ||||
| @ -111,7 +113,9 @@ func (g *Gorm) Info(ctx context.Context, s string, i ...any) { | ||||
| // | ||||
| // Date : 10:16 下午 2021/3/1 | ||||
| func (g *Gorm) Warn(ctx context.Context, s string, i ...any) { | ||||
| 	g.write(ctx, s, consts.LogLevelWarn, []zap.Field{zap.Any("log_data", i)}) | ||||
| 	g.write(ctx, s, consts.LogLevelWarn, map[string]any{ | ||||
| 		"log_data": i, | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| // Error 日志 | ||||
| @ -120,7 +124,9 @@ func (g *Gorm) Warn(ctx context.Context, s string, i ...any) { | ||||
| // | ||||
| // Date : 10:18 下午 2021/3/1 | ||||
| func (g *Gorm) Error(ctx context.Context, s string, i ...any) { | ||||
| 	g.write(ctx, s, consts.LogLevelError, []zap.Field{zap.Any("log_data", i)}) | ||||
| 	g.write(ctx, s, consts.LogLevelError, map[string]any{ | ||||
| 		"log_data": i, | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| // Trace Trace 记录 | ||||
| @ -137,17 +143,16 @@ func (g *Gorm) Trace(ctx context.Context, begin time.Time, fc func() (string, in | ||||
| 		sql, affectRows = fc() | ||||
| 	} | ||||
|  | ||||
| 	dataList := []zap.Field{ | ||||
| 		zap.String("db_flag", g.flag), | ||||
| 		zap.String("db_node", g.node), | ||||
| 		zap.Int64("begin_time", start), | ||||
| 		zap.Int64("finish_time", end), | ||||
| 		zap.Any("used_time", (end-start)/1e6), | ||||
| 		zap.String("sql", sql), | ||||
| 		zap.Int64("affect_rows", affectRows), | ||||
| 		zap.Error(err), | ||||
| 	} | ||||
| 	g.write(ctx, "SQL执行记录", consts.LogLevelInfo, dataList) | ||||
| 	g.write(ctx, "SQL执行记录", consts.LogLevelInfo, map[string]any{ | ||||
| 		"db_flag":     g.flag, | ||||
| 		"db_node":     g.node, | ||||
| 		"begin_time":  start, | ||||
| 		"finish_time": end, | ||||
| 		"used_time":   (end - start) / 1e6, | ||||
| 		"sql":         sql, | ||||
| 		"affect_rows": affectRows, | ||||
| 		"err":         err.Error(), | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| // write ... | ||||
| @ -155,7 +160,7 @@ func (g *Gorm) Trace(ctx context.Context, begin time.Time, fc func() (string, in | ||||
| // Author : go_developer@163.com<白茶清欢> | ||||
| // | ||||
| // Date : 4:11 PM 2021/12/24 | ||||
| func (g *Gorm) write(ctx context.Context, message string, level string, dataList []zap.Field) { | ||||
| func (g *Gorm) write(ctx context.Context, message string, level string, data map[string]any) { | ||||
| 	if len(message) == 0 { | ||||
| 		message = "SQL执行记录" | ||||
| 	} | ||||
| @ -163,8 +168,8 @@ func (g *Gorm) write(ctx context.Context, message string, level string, dataList | ||||
| 		// 未设置日志实例 | ||||
| 		return | ||||
| 	} | ||||
| 	if nil == dataList { | ||||
| 		dataList = make([]zap.Field, 0) | ||||
| 	if nil == data { | ||||
| 		data = make(map[string]any) | ||||
| 	} | ||||
| 	if nil == g.outCtx { | ||||
| 		g.outCtx = context.Background() | ||||
| @ -177,9 +182,12 @@ func (g *Gorm) write(ctx context.Context, message string, level string, dataList | ||||
| 		if nil == val { | ||||
| 			val = "" | ||||
| 		} | ||||
| 		dataList = append(dataList, zap.Any(extraField, val)) | ||||
| 		if val, exist := data[extraField]; !exist || nil == val { | ||||
| 			data[extraField] = val | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	dataList := logger2.ZapLogDataList(logger2.NewLogData(consts.LogTypeDatabase, "", data)) | ||||
| 	switch strings.ToUpper(level) { | ||||
| 	case consts.LogLevelDebug: | ||||
| 		g.instance.Debug(message, dataList...) | ||||
|  | ||||
		Reference in New Issue
	
	Block a user