2021-12-27 09:36:32 +08:00
|
|
|
package logger
|
|
|
|
|
2022-03-17 10:33:47 +08:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2022-05-18 18:11:43 +08:00
|
|
|
"go.uber.org/zap"
|
2022-03-17 10:33:47 +08:00
|
|
|
gl "gorm.io/gorm/logger"
|
|
|
|
"time"
|
|
|
|
)
|
2021-12-27 09:36:32 +08:00
|
|
|
|
2022-05-18 18:11:43 +08:00
|
|
|
var gormZap *zap.SugaredLogger
|
|
|
|
|
2021-12-27 09:36:32 +08:00
|
|
|
// 基于Gorm的日志实现
|
2022-03-17 10:33:47 +08:00
|
|
|
type gormLogger struct {
|
|
|
|
gl.Config
|
|
|
|
}
|
|
|
|
|
|
|
|
// LogMode 实现LogMode接口
|
|
|
|
func (l *gormLogger) LogMode(level gl.LogLevel) gl.Interface {
|
|
|
|
nl := *l
|
|
|
|
nl.LogLevel = level
|
|
|
|
return &nl
|
|
|
|
}
|
|
|
|
|
|
|
|
// Info 实现Info接口
|
|
|
|
func (l gormLogger) Info(ctx context.Context, msg string, data ...interface{}) {
|
|
|
|
if l.LogLevel >= gl.Info {
|
|
|
|
// // 去掉第一行
|
|
|
|
// msg = strings.Join(strings.Split(msg, "\n")[1:], " ")
|
2022-05-18 18:11:43 +08:00
|
|
|
// gormZap.Info(msg)
|
2022-03-17 10:33:47 +08:00
|
|
|
//
|
|
|
|
// l.Printf(msg, append([]interface{}{utils.FileWithLineNum()}, data...)...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Warn 实现Warn接口
|
|
|
|
func (l gormLogger) Warn(ctx context.Context, msg string, data ...interface{}) {
|
|
|
|
if l.LogLevel >= gl.Warn {
|
|
|
|
//
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 实现Error接口
|
|
|
|
func (l gormLogger) Error(ctx context.Context, msg string, data ...interface{}) {
|
|
|
|
if l.LogLevel >= gl.Error {
|
|
|
|
//
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Trace 实现Trace接口
|
|
|
|
func (l gormLogger) Trace(ctx context.Context, begin time.Time, fc func() (string, int64), err error) {
|
|
|
|
if l.LogLevel <= gl.Silent {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
elapsed := time.Since(begin)
|
|
|
|
sql, rows := fc()
|
|
|
|
msg := fmt.Sprintf("[%v] [rows:%v] %s", elapsed.String(), rows, sql)
|
|
|
|
if rows == -1 {
|
2022-03-17 10:48:11 +08:00
|
|
|
msg = fmt.Sprintf("[%s] [-] %s", elapsed.String(), sql)
|
2022-03-17 10:33:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case err != nil && l.LogLevel >= gl.Error && (!errors.Is(err, gl.ErrRecordNotFound) || !l.IgnoreRecordNotFoundError):
|
2022-05-18 18:11:43 +08:00
|
|
|
gormZap.Errorf("%s -> %s", err.Error(), sql)
|
2022-03-17 10:33:47 +08:00
|
|
|
case elapsed > l.SlowThreshold && l.SlowThreshold != 0 && l.LogLevel >= gl.Warn:
|
|
|
|
slowLog := fmt.Sprintf("SLOW SQL >= %v", l.SlowThreshold)
|
2022-05-18 18:11:43 +08:00
|
|
|
gormZap.Warnf("%v -> %v", slowLog, sql)
|
2022-03-17 10:33:47 +08:00
|
|
|
case l.LogLevel == gl.Info:
|
2022-05-18 18:11:43 +08:00
|
|
|
gormZap.Info(msg)
|
2022-03-17 10:33:47 +08:00
|
|
|
}
|
|
|
|
}
|
2021-12-27 09:36:32 +08:00
|
|
|
|
2022-03-17 10:33:47 +08:00
|
|
|
// NewGormLoggerWithConfig ...
|
|
|
|
func NewGormLoggerWithConfig(config gl.Config) gl.Interface {
|
|
|
|
return &gormLogger{config}
|
2021-12-27 09:36:32 +08:00
|
|
|
}
|
|
|
|
|
2022-03-17 10:33:47 +08:00
|
|
|
// DefaultGormLogger 默认的日志实现
|
|
|
|
func DefaultGormLogger() gl.Interface {
|
|
|
|
return &gormLogger{gl.Config{
|
|
|
|
SlowThreshold: time.Second, // Slow SQL threshold
|
|
|
|
IgnoreRecordNotFoundError: false, // 忽略没找到结果的错误
|
|
|
|
LogLevel: gl.Info, // Log level
|
|
|
|
Colorful: false, // Disable color
|
|
|
|
}}
|
2021-12-27 09:36:32 +08:00
|
|
|
}
|