升级数据库日志

This commit is contained in:
2024-07-24 22:03:22 +08:00
parent 77de4c7fd9
commit 0bc05b3816
4 changed files with 35 additions and 27 deletions

View File

@ -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...)