update logger
This commit is contained in:
@ -9,12 +9,10 @@ package wrapper
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"git.zhangdeman.cn/zhangdeman/consts"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"gorm.io/gorm"
|
||||
|
||||
"go.uber.org/zap/zapcore"
|
||||
@ -56,7 +54,7 @@ func NewGormV2(loggerLevel zapcore.Level, consoleOutput bool, encoder zapcore.En
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 3:36 PM 2021/12/24
|
||||
func NewGormLoggerWithInstance(ctx *gin.Context, dbClient *gorm.DB, instance *zap.Logger, node string, extraCtxFieldList []string) logger.Interface {
|
||||
func NewGormLoggerWithInstance(dbClient *gorm.DB, instance *zap.Logger, node string, extraCtxFieldList []string) logger.Interface {
|
||||
nodeArr := strings.Split(node, "|")
|
||||
i := &Gorm{
|
||||
dbClient: dbClient,
|
||||
@ -65,7 +63,6 @@ func NewGormLoggerWithInstance(ctx *gin.Context, dbClient *gorm.DB, instance *za
|
||||
extraCtxFieldList: extraCtxFieldList,
|
||||
flag: "",
|
||||
node: node,
|
||||
ctx: ctx,
|
||||
}
|
||||
if len(nodeArr) >= 2 {
|
||||
i.node = nodeArr[1]
|
||||
@ -81,12 +78,11 @@ func NewGormLoggerWithInstance(ctx *gin.Context, dbClient *gorm.DB, instance *za
|
||||
// Date : 9:55 下午 2021/3/1
|
||||
type Gorm struct {
|
||||
dbClient *gorm.DB
|
||||
instance *zap.Logger // 日志实例
|
||||
traceIDField string // 串联请求上下文的的ID
|
||||
extraCtxFieldList []string // 从请求上线问提取的字段
|
||||
flag string // 数据库标识
|
||||
node string // 数据库节点 master / slave
|
||||
ctx *gin.Context // gin上下文
|
||||
instance *zap.Logger // 日志实例
|
||||
traceIDField string // 串联请求上下文的的ID
|
||||
extraCtxFieldList []string // 从请求上线问提取的字段
|
||||
flag string // 数据库标识
|
||||
node string // 数据库节点 master / slave
|
||||
}
|
||||
|
||||
// LogMode ...
|
||||
@ -103,8 +99,8 @@ func (g *Gorm) LogMode(level logger.LogLevel) logger.Interface {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 10:18 下午 2021/3/1
|
||||
func (g *Gorm) Info(ctx context.Context, s string, i ...interface{}) {
|
||||
g.write(nil, "info")
|
||||
func (g *Gorm) Info(ctx context.Context, s string, i ...any) {
|
||||
g.write(ctx, s, consts.LogLevelInfo, []zap.Field{zap.Any("log_data", i)})
|
||||
}
|
||||
|
||||
// Warn ...
|
||||
@ -112,8 +108,8 @@ func (g *Gorm) Info(ctx context.Context, s string, i ...interface{}) {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 10:16 下午 2021/3/1
|
||||
func (g *Gorm) Warn(ctx context.Context, s string, i ...interface{}) {
|
||||
g.write(nil, "warn")
|
||||
func (g *Gorm) Warn(ctx context.Context, s string, i ...any) {
|
||||
g.write(ctx, s, consts.LogLevelWarn, []zap.Field{zap.Any("log_data", i)})
|
||||
}
|
||||
|
||||
// Error 日志
|
||||
@ -121,8 +117,8 @@ func (g *Gorm) Warn(ctx context.Context, s string, i ...interface{}) {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 10:18 下午 2021/3/1
|
||||
func (g *Gorm) Error(ctx context.Context, s string, i ...interface{}) {
|
||||
g.write(nil, "error")
|
||||
func (g *Gorm) Error(ctx context.Context, s string, i ...any) {
|
||||
g.write(ctx, s, consts.LogLevelError, []zap.Field{zap.Any("log_data", i)})
|
||||
}
|
||||
|
||||
// Trace Trace 记录
|
||||
@ -144,15 +140,12 @@ func (g *Gorm) Trace(ctx context.Context, begin time.Time, fc func() (string, in
|
||||
zap.String("db_node", g.node),
|
||||
zap.Int64("begin_time", start),
|
||||
zap.Int64("finish_time", end),
|
||||
zap.String("used_time", fmt.Sprintf("%fms", float64(end-start)/1e6)),
|
||||
zap.Any("used_time", (end-start)/1e6),
|
||||
zap.String("sql", sql),
|
||||
zap.Int64("affect_rows", affectRows),
|
||||
zap.Error(err),
|
||||
}
|
||||
if len(g.traceIDField) > 0 {
|
||||
dataList = append(dataList, zap.String(g.traceIDField, g.getTraceID(ctx)))
|
||||
}
|
||||
g.write(dataList, "info")
|
||||
g.write(ctx, "SQL执行记录", consts.LogLevelInfo, dataList)
|
||||
}
|
||||
|
||||
// write ...
|
||||
@ -160,7 +153,10 @@ 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(dataList []zap.Field, level string) {
|
||||
func (g *Gorm) write(ctx context.Context, message string, level string, dataList []zap.Field) {
|
||||
if len(message) == 0 {
|
||||
message = "SQL执行记录"
|
||||
}
|
||||
if nil == g.instance {
|
||||
// 未设置日志实例
|
||||
return
|
||||
@ -168,40 +164,32 @@ func (g *Gorm) write(dataList []zap.Field, level string) {
|
||||
if nil == dataList {
|
||||
dataList = make([]zap.Field, 0)
|
||||
}
|
||||
if nil != g.ctx {
|
||||
for _, extraField := range g.extraCtxFieldList {
|
||||
if len(extraField) == 0 {
|
||||
continue
|
||||
}
|
||||
dataList = append(dataList, zap.Any(extraField, g.ctx.Value(extraField)))
|
||||
if nil == ctx {
|
||||
ctx = context.Background()
|
||||
}
|
||||
for _, extraField := range g.extraCtxFieldList {
|
||||
if len(extraField) == 0 {
|
||||
continue
|
||||
}
|
||||
dataList = append(dataList, zap.Any(extraField, ctx.Value(extraField)))
|
||||
}
|
||||
|
||||
message := "SQL执行记录"
|
||||
switch strings.ToLower(level) {
|
||||
case "info":
|
||||
switch strings.ToUpper(level) {
|
||||
case consts.LogLevelDebug:
|
||||
g.instance.Debug(message, dataList...)
|
||||
case consts.LogLevelInfo:
|
||||
g.instance.Info(message, dataList...)
|
||||
case "warn":
|
||||
case consts.LogLevelWarn:
|
||||
g.instance.Warn(message, dataList...)
|
||||
case "error":
|
||||
case consts.LogLevelError:
|
||||
g.instance.Error(message, dataList...)
|
||||
case consts.LogLevelPanic:
|
||||
g.instance.Panic(message, dataList...)
|
||||
default:
|
||||
g.instance.Info(message, dataList...)
|
||||
}
|
||||
}
|
||||
|
||||
// getTraceID 获取traceID
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 10:11 下午 2021/3/1
|
||||
func (g *Gorm) getTraceID(ctx context.Context) string {
|
||||
if nil == g.ctx {
|
||||
return ""
|
||||
}
|
||||
return g.ctx.GetString(g.traceIDField)
|
||||
}
|
||||
|
||||
// GetGormSQL 获取trace fn
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
|
@ -8,10 +8,8 @@
|
||||
package wrapper
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"context"
|
||||
"git.zhangdeman.cn/zhangdeman/logger"
|
||||
"github.com/gin-gonic/gin"
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zapcore"
|
||||
)
|
||||
@ -55,18 +53,16 @@ type Gin struct {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 4:13 下午 2021/1/3
|
||||
func (gw *Gin) formatFieldList(ginCtx *gin.Context, inputFieldList []zap.Field) []zap.Field {
|
||||
func (gw *Gin) formatFieldList(ctx context.Context, inputFieldList []zap.Field) []zap.Field {
|
||||
if nil == ctx {
|
||||
ctx = context.Background()
|
||||
}
|
||||
if nil == inputFieldList {
|
||||
inputFieldList = make([]zap.Field, 0)
|
||||
}
|
||||
if nil != ginCtx {
|
||||
// 自动扩充抽取字段,字段不存在的话,忽略掉
|
||||
for _, extractField := range gw.extractFieldList {
|
||||
if v, exist := ginCtx.Get(extractField); exist {
|
||||
byteData, _ := json.Marshal(v)
|
||||
inputFieldList = append(inputFieldList, zap.String(extractField, string(byteData)))
|
||||
}
|
||||
}
|
||||
// 自动扩充抽取字段,字段不存在的话,忽略掉
|
||||
for _, extractField := range gw.extractFieldList {
|
||||
inputFieldList = append(inputFieldList, zap.Any(extractField, ctx.Value(extractField)))
|
||||
}
|
||||
return inputFieldList
|
||||
}
|
||||
@ -76,8 +72,8 @@ func (gw *Gin) formatFieldList(ginCtx *gin.Context, inputFieldList []zap.Field)
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 4:14 下午 2021/1/3
|
||||
func (gw *Gin) Debug(ginCtx *gin.Context, msg string, field ...zap.Field) {
|
||||
fieldList := gw.formatFieldList(ginCtx, field)
|
||||
func (gw *Gin) Debug(ctx context.Context, msg string, field ...zap.Field) {
|
||||
fieldList := gw.formatFieldList(ctx, field)
|
||||
gw.loggerInstance.Debug(msg, fieldList...)
|
||||
}
|
||||
|
||||
@ -86,8 +82,8 @@ func (gw *Gin) Debug(ginCtx *gin.Context, msg string, field ...zap.Field) {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 4:28 下午 2021/1/3
|
||||
func (gw *Gin) Info(ginCtx *gin.Context, msg string, field ...zap.Field) {
|
||||
fieldList := gw.formatFieldList(ginCtx, field)
|
||||
func (gw *Gin) Info(ctx context.Context, msg string, field ...zap.Field) {
|
||||
fieldList := gw.formatFieldList(ctx, field)
|
||||
gw.loggerInstance.Info(msg, fieldList...)
|
||||
}
|
||||
|
||||
@ -96,8 +92,8 @@ func (gw *Gin) Info(ginCtx *gin.Context, msg string, field ...zap.Field) {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 4:29 下午 2021/1/3
|
||||
func (gw *Gin) Warn(ginCtx *gin.Context, msg string, field ...zap.Field) {
|
||||
fieldList := gw.formatFieldList(ginCtx, field)
|
||||
func (gw *Gin) Warn(ctx context.Context, msg string, field ...zap.Field) {
|
||||
fieldList := gw.formatFieldList(ctx, field)
|
||||
gw.loggerInstance.Warn(msg, fieldList...)
|
||||
}
|
||||
|
||||
@ -106,8 +102,8 @@ func (gw *Gin) Warn(ginCtx *gin.Context, msg string, field ...zap.Field) {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 4:29 下午 2021/1/3
|
||||
func (gw *Gin) Error(ginCtx *gin.Context, msg string, field ...zap.Field) {
|
||||
fieldList := gw.formatFieldList(ginCtx, field)
|
||||
func (gw *Gin) Error(ctx context.Context, msg string, field ...zap.Field) {
|
||||
fieldList := gw.formatFieldList(ctx, field)
|
||||
gw.loggerInstance.Error(msg, fieldList...)
|
||||
}
|
||||
|
||||
@ -116,8 +112,8 @@ func (gw *Gin) Error(ginCtx *gin.Context, msg string, field ...zap.Field) {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 4:29 下午 2021/1/3
|
||||
func (gw *Gin) Panic(ginCtx *gin.Context, msg string, field ...zap.Field) {
|
||||
fieldList := gw.formatFieldList(ginCtx, field)
|
||||
func (gw *Gin) Panic(ctx context.Context, msg string, field ...zap.Field) {
|
||||
fieldList := gw.formatFieldList(ctx, field)
|
||||
gw.loggerInstance.Panic(msg, fieldList...)
|
||||
}
|
||||
|
||||
@ -126,8 +122,8 @@ func (gw *Gin) Panic(ginCtx *gin.Context, msg string, field ...zap.Field) {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 4:30 下午 2021/1/3
|
||||
func (gw *Gin) DPanic(ginCtx *gin.Context, msg string, field ...zap.Field) {
|
||||
fieldList := gw.formatFieldList(ginCtx, field)
|
||||
func (gw *Gin) DPanic(ctx context.Context, msg string, field ...zap.Field) {
|
||||
fieldList := gw.formatFieldList(ctx, field)
|
||||
gw.loggerInstance.DPanic(msg, fieldList...)
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user