2 Star 1 Fork 0

法马智慧/fmgo

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
gorm.go 2.92 KB
一键复制 编辑 原始数据 按行查看 历史
零海 提交于 2023-03-14 15:51 . 调整日志追溯等级
package logx
import (
"context"
"errors"
"fmt"
"go.uber.org/zap/zapcore"
"gorm.io/gorm"
glogger "gorm.io/gorm/logger"
"gorm.io/gorm/utils"
"time"
)
type GormLogger struct {
glogger.Interface
glogger.Config
infoStr, warnStr, errStr string
traceStr, traceErrStr, traceWarnStr string
*Logger
}
// NewGormLogger returns a new GormLogger instance.
func NewGormLogger(config glogger.Config) *GormLogger {
var (
infoStr = "[info] "
warnStr = "[warn] "
errStr = "[error] "
traceStr = "[%.3fms] [rows:%v] %s"
traceWarnStr = "%s\t[%.3fms] [rows:%v] %s"
traceErrStr = "%s\t[%.3fms] [rows:%v] %s"
)
return &GormLogger{
Config: config,
infoStr: infoStr,
warnStr: warnStr,
errStr: errStr,
traceStr: traceStr,
traceWarnStr: traceWarnStr,
traceErrStr: traceErrStr,
Logger: logger,
}
}
var gormDeep = 5
// LogMode log mode
func (l *GormLogger) LogMode(level glogger.LogLevel) glogger.Interface {
l.Logger.Logger.Core().Enabled(zapcore.Level(level))
return l.Interface
}
// Info print info
func (l *GormLogger) Info(ctx context.Context, msg string, data ...interface{}) {
if logLevel > InfoLevel {
return
}
l.Logger.Info(gormDeep, fmt.Sprintf(msg, data...))
}
// Warn print warn messages
func (l *GormLogger) Warn(ctx context.Context, msg string, data ...interface{}) {
if logLevel > WarnLevel {
return
}
l.Logger.Warn(gormDeep, fmt.Sprintf(msg, data...))
}
// Error print error messages
func (l *GormLogger) Error(ctx context.Context, msg string, data ...interface{}) {
if logLevel > ErrorLevel {
return
}
l.Logger.Error(gormDeep, fmt.Sprintf(msg, data...))
}
// Trace print sql message
func (l *GormLogger) Trace(ctx context.Context, begin time.Time, fc func() (string, int64), err error) {
elapsed := time.Since(begin)
switch {
case err != nil && logLevel <= zapcore.ErrorLevel && (!errors.Is(err, gorm.ErrRecordNotFound) || !l.IgnoreRecordNotFoundError):
sql, rows := fc()
if rows == -1 {
l.Logger.Error(gormDeep, fmt.Sprintf(l.traceErrStr, utils.FileWithLineNum(), err, float64(elapsed.Nanoseconds())/1e6, "-", sql))
} else {
l.Logger.Error(gormDeep, fmt.Sprintf(l.traceErrStr, err, float64(elapsed.Nanoseconds())/1e6, rows, sql))
}
case elapsed > l.SlowThreshold && l.SlowThreshold != 0 && logLevel <= zapcore.WarnLevel:
sql, rows := fc()
slowLog := fmt.Sprintf("SLOW SQL >= %v", l.SlowThreshold)
if rows == -1 {
l.Logger.Warn(gormDeep, fmt.Sprintf(l.traceWarnStr, slowLog, float64(elapsed.Nanoseconds())/1e6, "-", sql))
} else {
l.Logger.Warn(gormDeep, fmt.Sprintf(l.traceWarnStr, slowLog, float64(elapsed.Nanoseconds())/1e6, rows, sql))
}
case logLevel <= zapcore.InfoLevel:
sql, rows := fc()
if rows == -1 {
l.Logger.Info(gormDeep, fmt.Sprintf(l.traceStr, float64(elapsed.Nanoseconds())/1e6, "-", sql))
} else {
l.Logger.Info(gormDeep, fmt.Sprintf(l.traceStr, float64(elapsed.Nanoseconds())/1e6, rows, sql))
}
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/fmpt/fmgo.git
git@gitee.com:fmpt/fmgo.git
fmpt
fmgo
fmgo
v1.2.56

搜索帮助