代码拉取完成,页面将自动刷新
package log
import (
"context"
"errors"
"fmt"
"gitee.com/wuzheng0709/backend-gopkg/infrastructure/config"
"gitee.com/wuzheng0709/backend-gopkg/infrastructure/pkg/gin/log/logger"
gormLogger "gorm.io/gorm/logger"
gormUtil "gorm.io/gorm/utils"
"time"
)
var (
TeamsLog *logger.Logger
Config *logger.Config
)
func init() {
if config.C.Debug {
Config = logger.DebugConfig()
TeamsLog = logger.New(Config)
} else {
TeamsLog = logger.New(nil)
Config = TeamsLog.Config
}
}
// 应用更新配置文件
func WithConfig() {
TeamsLog.WithConfig()
}
func Debug(args ...interface{}) {
TeamsLog.Debug(args...)
}
func Debugf(template string, args ...interface{}) {
TeamsLog.Debugf(template, args...)
}
func Info(args ...interface{}) {
TeamsLog.Info(args...)
}
func Infof(template string, args ...interface{}) {
TeamsLog.Infof(template, args...)
}
func Warn(args ...interface{}) {
TeamsLog.Warn(args...)
}
func Warnf(template string, args ...interface{}) {
TeamsLog.Warnf(template, args...)
}
func Error(args ...interface{}) {
TeamsLog.Error(args...)
}
func Errorf(template string, args ...interface{}) {
TeamsLog.Errorf(template, args...)
}
func Fatal(args ...interface{}) {
TeamsLog.Fatal(args...)
}
func Fatalf(template string, args ...interface{}) {
TeamsLog.Fatalf(template, args...)
}
func Panic(args ...interface{}) {
TeamsLog.Panic(args...)
}
func Panicf(template string, args ...interface{}) {
TeamsLog.Panicf(template, args...)
}
type GormLog struct {
gormLogger.Config
gormLogger.LogLevel
infoStr, warnStr, errStr string
traceStr, traceErrStr, traceWarnStr string
}
func NewGorm() *GormLog {
var (
//infoStr = "%s\n[info] "
//warnStr = "%s\n[warn] "
//errStr = "%s\n[error] "
//traceStr = "%s\n[%.3fms] [rows:%v] %s"
//traceWarnStr = "%s %s\n[%.3fms] [rows:%v] %s"
//traceErrStr = "%s %s\n[%.3fms] [rows:%v] %s"
infoStr = "{\"Sql\":\"【info】:%s\"}"
warnStr = "{\"Sql\":\"【warn】:%s\"}"
errStr = "{\"Sql\":\"【err】:%s\"}"
traceStr = "{\"【info】FileWithLineNum\":\"%s\", \"SqlTimeMs\":%.3f, \"Row\":\"%v\", \"Sql\":\"%s\"}"
traceWarnStr = "{\"【warn】FileWithLineNum\":\"%s\", \"SlowSql\":\"%s\", \"SqlTimeMs\":%.3f, \"Row\":\"%v\", \"Sql\":\"%s\"}"
traceErrStr = "{\"【err】FileWithLineNum\":\"%s\", \"Err\":\"%s\", \"SqlTimeMs\":%.3f, \"Row\":\"%v\", \"Sql\":\"%s\"}"
)
return &GormLog{
infoStr: infoStr,
warnStr: warnStr,
errStr: errStr,
traceStr: traceStr,
traceWarnStr: traceWarnStr,
traceErrStr: traceErrStr,
Config: gormLogger.Config{
SlowThreshold: 200 * time.Millisecond, // Slow SQL threshold
LogLevel: gormLogger.Info, // Log level
IgnoreRecordNotFoundError: true, // Ignore ErrRecordNotFound error for logger
},
}
}
func (gl *GormLog) LogMode(level gormLogger.LogLevel) gormLogger.Interface {
gl.LogLevel = level
return gl
}
func (gl *GormLog) Info(ctx context.Context, msg string, arg ...interface{}) {
TeamsLog.Infof(gl.infoStr+msg, append([]interface{}{gormUtil.FileWithLineNum()}, arg...)...)
}
func (gl *GormLog) Warn(ctx context.Context, msg string, arg ...interface{}) {
TeamsLog.Warnf(gl.warnStr+msg, append([]interface{}{gormUtil.FileWithLineNum()}, arg...)...)
}
func (gl *GormLog) Error(ctx context.Context, msg string, arg ...interface{}) {
TeamsLog.Errorf(gl.errStr+msg, append([]interface{}{gormUtil.FileWithLineNum()}, arg...)...)
}
func (gl *GormLog) Trace(ctx context.Context, begin time.Time, fc func() (sql string, rowsAffected int64), err error) {
elapsed := time.Since(begin)
switch {
case err != nil && gl.LogLevel >= gormLogger.Error && (!errors.Is(err, gormLogger.ErrRecordNotFound) || !gl.IgnoreRecordNotFoundError):
sql, rows := fc()
if rows == -1 {
TeamsLog.Errorf(gl.traceErrStr, gormUtil.FileWithLineNum(), err.Error(), float64(elapsed.Nanoseconds())/1e6, "-", sql)
} else {
TeamsLog.Errorf(gl.traceErrStr, gormUtil.FileWithLineNum(), err.Error(), float64(elapsed.Nanoseconds())/1e6, rows, sql)
}
case elapsed > gl.SlowThreshold && gl.SlowThreshold != 0 && gl.LogLevel >= gormLogger.Warn:
sql, rows := fc()
slowLog := fmt.Sprintf("SLOW SQL >= %v", gl.SlowThreshold)
if rows == -1 {
TeamsLog.Warnf(gl.traceWarnStr, gormUtil.FileWithLineNum(), slowLog, float64(elapsed.Nanoseconds())/1e6, "-", sql)
} else {
TeamsLog.Warnf(gl.traceWarnStr, gormUtil.FileWithLineNum(), slowLog, float64(elapsed.Nanoseconds())/1e6, rows, sql)
}
case gl.LogLevel == gormLogger.Info:
sql, rows := fc()
if rows == -1 {
TeamsLog.Infof(gl.traceStr, gormUtil.FileWithLineNum(), float64(elapsed.Nanoseconds())/1e6, "-", sql)
} else {
TeamsLog.Infof(gl.traceStr, gormUtil.FileWithLineNum(), float64(elapsed.Nanoseconds())/1e6, rows, sql)
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。