代码拉取完成,页面将自动刷新
package component
import (
"context"
"errors"
"fmt"
glogger "gitee.com/kzangv/gsf-fof/logger"
"gorm.io/gorm/logger"
"gorm.io/gorm/utils"
"time"
)
const (
_TraceStr = "\n\t%s\n\t[%.3fms] [rows:%v] %s"
_TraceErrStr = "\n\t%s %s\n\t[%.3fms] [rows:%v] %s"
_TraceWarnStr = "\n\t%s %s\n\t[%.3fms] [rows:%v] %s"
)
type MysqlLogger struct {
log glogger.Interface
level logger.LogLevel
slowThreshold time.Duration
}
func (l *MysqlLogger) SetLogger(log glogger.Interface) {
l.log = log
}
func (l *MysqlLogger) LogMode(level logger.LogLevel) logger.Interface {
l.level = level
return l
}
func (l *MysqlLogger) Info(_ context.Context, msg string, data ...interface{}) {
if l.level >= logger.Info {
l.log.Info(msg, append([]interface{}{utils.FileWithLineNum()}, data...)...)
}
}
func (l *MysqlLogger) Warn(_ context.Context, msg string, data ...interface{}) {
if l.level >= logger.Warn {
l.log.Warn(msg, append([]interface{}{utils.FileWithLineNum()}, data...)...)
}
}
func (l *MysqlLogger) Error(_ context.Context, msg string, data ...interface{}) {
if l.level >= logger.Error {
l.log.Error(msg, append([]interface{}{utils.FileWithLineNum()}, data...)...)
}
}
func (l *MysqlLogger) Trace(_ context.Context, begin time.Time, fc func() (string, int64), err error) {
if l.level <= logger.Silent {
return
}
elapsed := time.Since(begin)
switch {
case err != nil && l.level >= logger.Error && !errors.Is(err, logger.ErrRecordNotFound):
sql, rows := fc()
if rows == -1 {
l.log.Error(_TraceErrStr, utils.FileWithLineNum(), err, float64(elapsed.Nanoseconds())/1e6, "-", sql)
} else {
l.log.Error(_TraceErrStr, utils.FileWithLineNum(), err, float64(elapsed.Nanoseconds())/1e6, rows, sql)
}
case elapsed > l.slowThreshold && l.slowThreshold != 0 && l.level >= logger.Warn:
sql, rows := fc()
slowLog := fmt.Sprintf("SLOW SQL >= %v", l.slowThreshold)
if rows == -1 {
l.log.Warn(_TraceWarnStr, utils.FileWithLineNum(), slowLog, float64(elapsed.Nanoseconds())/1e6, "-", sql)
} else {
l.log.Warn(_TraceWarnStr, utils.FileWithLineNum(), slowLog, float64(elapsed.Nanoseconds())/1e6, rows, sql)
}
case l.level == logger.Info:
sql, rows := fc()
if rows == -1 {
l.log.Info(_TraceStr, utils.FileWithLineNum(), float64(elapsed.Nanoseconds())/1e6, "-", sql)
} else {
l.log.Info(_TraceStr, utils.FileWithLineNum(), float64(elapsed.Nanoseconds())/1e6, rows, sql)
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。