1 Star 0 Fork 0

h79/goutils

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
logger.go 3.41 KB
一键复制 编辑 原始数据 按行查看 历史
huqiuyun 提交于 2023-08-16 15:28 . SQL LOG
package db
import (
"context"
"errors"
"fmt"
"gitee.com/h79/goutils/common/debug"
"gitee.com/h79/goutils/common/logger"
"gitee.com/h79/goutils/common/result"
"gitee.com/h79/goutils/dao/config"
"gitee.com/h79/goutils/plugins"
"go.uber.org/zap"
"gorm.io/gorm"
gormLogger "gorm.io/gorm/logger"
"strconv"
"strings"
"time"
)
type Logger struct {
config.SqlLogger
}
// LogMode log mode
func (l *Logger) LogMode(level gormLogger.LogLevel) gormLogger.Interface {
newLogger := *l
newLogger.LogLevel = int(level)
return &newLogger
}
// Info print info
func (l *Logger) Info(ctx context.Context, msg string, data ...interface{}) {
if l.check(gormLogger.Info) {
logger.Info(msg, data...)
}
}
// Warn print warn messages
func (l *Logger) Warn(ctx context.Context, msg string, data ...interface{}) {
if l.check(gormLogger.Warn) {
logger.Warn(msg, data...)
}
}
// Error print error messages
func (l *Logger) Error(ctx context.Context, msg string, data ...interface{}) {
if l.check(gormLogger.Error) {
logger.Error(msg, data...)
}
}
// Trace print sql message
func (l *Logger) Trace(ctx context.Context, begin time.Time, fc func() (string, int64), err error) {
elapsed := time.Now().Sub(begin)
switch {
case err != nil:
recordNotFound := errors.Is(err, gorm.ErrRecordNotFound)
logEnabled := l.check(gormLogger.Error) && (!recordNotFound || !l.IgnoreRecordNotFoundError)
if !logEnabled && !l.AlarmEnabled {
return
}
sql, rows := fc()
if logEnabled {
zap.L().Info("Trace",
zap.Error(err),
zap.Int64("rows", rows),
zap.String("sql", sql),
zap.String("speed", elapsed.String()))
}
if l.AlarmEnabled && !recordNotFound {
var b = strings.Builder{}
b.WriteString("sql: '")
b.WriteString(sql)
b.WriteString("',rows: '")
b.WriteString(strconv.Itoa(int(rows)))
b.WriteString("',speed: '")
b.WriteString(elapsed.String())
b.WriteString("'")
alarm(b.String(), err)
}
case elapsed > l.SlowThreshold && l.SlowThreshold != 0:
if !l.check(gormLogger.Warn) && !l.AlarmEnabled {
return
}
sql, rows := fc()
if l.check(gormLogger.Warn) {
zap.L().Warn("Trace",
zap.String("slow", fmt.Sprintf("SLOW SQL >= %v", l.SlowThreshold)),
zap.Int64("rows", rows),
zap.String("sql", sql),
zap.String("speed", elapsed.String()))
}
if l.AlarmEnabled {
var b = strings.Builder{}
b.WriteString("sql: '")
b.WriteString(sql)
b.WriteString("',rows: '")
b.WriteString(strconv.Itoa(int(rows)))
b.WriteString("',speed: '")
b.WriteString(elapsed.String())
b.WriteString("'")
alarm(b.String(), nil)
}
case l.check(gormLogger.Info):
sql, rows := fc()
zap.L().Info("Trace",
zap.Int64("rows", rows),
zap.String("sql", sql),
zap.String("speed", elapsed.String()))
}
}
func (l *Logger) check(level gormLogger.LogLevel) bool {
return gormLogger.LogLevel(l.LogLevel) >= level
}
type esLogger struct {
Level int
LogLevel int8
}
func (l *esLogger) Printf(format string, v ...interface{}) {
if l.LogLevel < int8(l.Level) {
return
}
switch l.LogLevel {
case logger.ErrorLevel:
logger.Error(format, v...)
case logger.DebugLevel:
logger.Debug(format, v...)
case logger.InfoLevel:
logger.Info(format, v...)
}
}
func alarm(detail string, err error) {
d := debug.New(result.ErrDbInternal).
WithDetail(detail).
WithError(err).
WithTitle("Sql").
WithLevel(debug.DFatalLevel)
_, _ = plugins.Do(plugins.KAlarm, context.Background(), d)
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/h79/goutils.git
git@gitee.com:h79/goutils.git
h79
goutils
goutils
v1.9.11

搜索帮助