logger/wrapper/gorm_v2.go

229 lines
5.5 KiB
Go
Raw Normal View History

2022-05-14 13:04:01 +08:00
// Package wrapper...
//
// Description : gorm v2 版本接口实现
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2021/03/01 9:52 下午
package wrapper
import (
"context"
2024-07-25 13:48:35 +08:00
"fmt"
2022-05-14 13:04:01 +08:00
"strings"
"time"
2024-06-30 19:24:07 +08:00
"git.zhangdeman.cn/zhangdeman/consts"
2022-05-14 13:04:01 +08:00
"gorm.io/gorm"
"go.uber.org/zap/zapcore"
"go.uber.org/zap"
logger2 "git.zhangdeman.cn/zhangdeman/logger"
"gorm.io/gorm/logger"
)
// NewGormV2 获取日志实例
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 9:56 下午 2021/3/1
2024-06-24 18:20:26 +08:00
func NewGormV2(loggerLevel string, consoleOutput bool, encoder zapcore.Encoder, splitConfig *logger2.RotateLogConfig, traceIDField string, skip int) (logger.Interface, error) {
2022-05-14 13:04:01 +08:00
logConfList := []logger2.SetLoggerOptionFunc{logger2.WithEncoder(encoder), logger2.WithCallerSkip(skip), logger2.WithCaller()}
if consoleOutput {
logConfList = append(logConfList, logger2.WithConsoleOutput())
}
logInstance, err := logger2.NewLogger(loggerLevel, splitConfig, logConfList...)
if nil != err {
return nil, err
}
if len(traceIDField) == 0 {
traceIDField = "trace_id"
}
return &Gorm{
instance: logInstance,
traceIDField: traceIDField,
}, nil
}
// NewGormLoggerWithInstance 获取gorm日志实现
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 3:36 PM 2021/12/24
2024-06-30 19:24:07 +08:00
func NewGormLoggerWithInstance(outCtx context.Context, dbClient *gorm.DB, instance *zap.Logger, node string, extraCtxFieldList []string) logger.Interface {
2023-02-12 16:06:14 +08:00
nodeArr := strings.Split(node, "|")
i := &Gorm{
2022-05-14 13:04:01 +08:00
dbClient: dbClient,
instance: instance,
2024-07-23 21:43:30 +08:00
traceIDField: consts.GinTraceIDField,
2022-05-14 13:04:01 +08:00
extraCtxFieldList: extraCtxFieldList,
flag: "",
node: node,
2024-06-30 19:24:07 +08:00
outCtx: outCtx,
2022-05-14 13:04:01 +08:00
}
2023-02-12 16:06:14 +08:00
if len(nodeArr) >= 2 {
i.node = nodeArr[1]
i.flag = nodeArr[0]
}
2023-02-12 16:08:29 +08:00
return i
2022-05-14 13:04:01 +08:00
}
// Gorm v2 版本库日志实现
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 9:55 下午 2021/3/1
type Gorm struct {
dbClient *gorm.DB
2024-06-24 17:24:21 +08:00
instance *zap.Logger // 日志实例
traceIDField string // 串联请求上下文的的ID
extraCtxFieldList []string // 从请求上线问提取的字段
flag string // 数据库标识
node string // 数据库节点 master / slave
2024-06-30 19:24:07 +08:00
outCtx context.Context
2022-05-14 13:04:01 +08:00
}
// LogMode ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:08 下午 2021/3/1
func (g *Gorm) LogMode(level logger.LogLevel) logger.Interface {
return g
}
// Info 日志
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:18 下午 2021/3/1
2024-06-24 17:24:21 +08:00
func (g *Gorm) Info(ctx context.Context, s string, i ...any) {
2024-07-24 22:03:22 +08:00
g.write(ctx, s, consts.LogLevelInfo, map[string]any{
"log_data": i,
})
2022-05-14 13:04:01 +08:00
}
// Warn ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:16 下午 2021/3/1
2024-06-24 17:24:21 +08:00
func (g *Gorm) Warn(ctx context.Context, s string, i ...any) {
2024-07-24 22:03:22 +08:00
g.write(ctx, s, consts.LogLevelWarn, map[string]any{
"log_data": i,
})
2022-05-14 13:04:01 +08:00
}
// Error 日志
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:18 下午 2021/3/1
2024-06-24 17:24:21 +08:00
func (g *Gorm) Error(ctx context.Context, s string, i ...any) {
2024-07-24 22:03:22 +08:00
g.write(ctx, s, consts.LogLevelError, map[string]any{
"log_data": i,
})
2022-05-14 13:04:01 +08:00
}
// Trace Trace 记录
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:19 下午 2021/3/1
func (g *Gorm) Trace(ctx context.Context, begin time.Time, fc func() (string, int64), err error) {
start := begin.UnixNano()
end := time.Now().UnixNano()
sql := ""
affectRows := int64(0)
if nil != fc {
sql, affectRows = fc()
}
2024-07-24 22:25:40 +08:00
logData := map[string]any{
2024-07-24 22:03:22 +08:00
"db_flag": g.flag,
"db_node": g.node,
"begin_time": start,
"finish_time": end,
"used_time": (end - start) / 1e6,
"sql": sql,
"affect_rows": affectRows,
2024-07-24 22:25:40 +08:00
"err": "",
}
if nil != err {
logData["err"] = err.Error()
}
g.write(ctx, "SQL执行记录", consts.LogLevelInfo, logData)
2022-05-14 13:04:01 +08:00
}
// write ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 4:11 PM 2021/12/24
2024-07-24 22:03:22 +08:00
func (g *Gorm) write(ctx context.Context, message string, level string, data map[string]any) {
2024-06-24 17:24:21 +08:00
if len(message) == 0 {
message = "SQL执行记录"
}
2022-05-14 13:04:01 +08:00
if nil == g.instance {
// 未设置日志实例
return
}
2024-07-24 22:03:22 +08:00
if nil == data {
data = make(map[string]any)
2022-05-14 13:04:01 +08:00
}
2024-07-25 13:48:35 +08:00
if nil != data["sql"] {
sqlStr := strings.TrimSpace(fmt.Sprintf("%v", data["sql"]))
sqlArr := strings.Split(sqlStr, " ")
if len(sqlArr) > 0 {
message = fmt.Sprintf("【%v】" + message)
}
}
2024-06-30 19:24:07 +08:00
if nil == g.outCtx {
g.outCtx = context.Background()
2024-06-24 17:24:21 +08:00
}
2024-07-24 22:58:05 +08:00
/*for _, extraField := range g.extraCtxFieldList {
2024-06-24 17:24:21 +08:00
if len(extraField) == 0 {
continue
2022-05-14 13:04:01 +08:00
}
2024-06-30 19:24:07 +08:00
val := g.outCtx.Value(extraField)
if nil == val {
val = ""
}
2024-07-24 22:03:22 +08:00
if val, exist := data[extraField]; !exist || nil == val {
data[extraField] = val
}
2024-07-24 22:58:05 +08:00
}*/
2022-05-14 13:04:01 +08:00
2024-07-24 22:43:09 +08:00
dataList := logger2.ZapLogDataList(logger2.NewLogData(g.outCtx, consts.LogTypeDatabase, "", data))
2024-06-24 17:24:21 +08:00
switch strings.ToUpper(level) {
case consts.LogLevelDebug:
g.instance.Debug(message, dataList...)
case consts.LogLevelInfo:
2022-05-14 13:04:01 +08:00
g.instance.Info(message, dataList...)
2024-06-24 17:24:21 +08:00
case consts.LogLevelWarn:
2022-05-14 13:04:01 +08:00
g.instance.Warn(message, dataList...)
2024-06-24 17:24:21 +08:00
case consts.LogLevelError:
2022-05-14 13:04:01 +08:00
g.instance.Error(message, dataList...)
2024-06-24 17:24:21 +08:00
case consts.LogLevelPanic:
g.instance.Panic(message, dataList...)
2022-05-14 13:04:01 +08:00
default:
g.instance.Info(message, dataList...)
}
}
// GetGormSQL 获取trace fn
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:38 下午 2021/3/1
func GetGormSQL(dbClient *gorm.DB) func() (string, int64) {
return func() (string, int64) {
return dbClient.Dialector.Explain(dbClient.Statement.SQL.String(), dbClient.Statement.Vars...), dbClient.RowsAffected
}
}