代码拉取完成,页面将自动刷新
package db
import (
"context"
"errors"
"fmt"
"gitee.com/h79/goutils/common/logger"
"gitee.com/h79/goutils/dao/config"
"gitee.com/h79/goutils/dao/util"
"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("'")
util.Alarm("Sql", 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("'")
util.Alarm("Sql", 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...)
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。