-
Notifications
You must be signed in to change notification settings - Fork 0
/
gormlog.go
92 lines (76 loc) · 2.17 KB
/
gormlog.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package gorm2logrus
import (
"context"
"errors"
"fmt"
"os"
"time"
"github.com/sirupsen/logrus"
"gorm.io/gorm"
gormlogger "gorm.io/gorm/logger"
"gorm.io/gorm/utils"
)
type GormLogrus struct {
logrus.Logger
// for gorm v2
SlowThreshold time.Duration
SourceField string
SkipErrRecordNotFound bool
}
func NewGormLogger() *GormLogrus {
return &GormLogrus{
Logger: logrus.Logger{
Out: os.Stderr,
Formatter: new(logrus.TextFormatter),
Hooks: make(logrus.LevelHooks),
Level: logrus.TraceLevel,
ExitFunc: os.Exit,
ReportCaller: false,
},
SkipErrRecordNotFound: false,
}
}
func (l *GormLogrus) LogMode(level gormlogger.LogLevel) gormlogger.Interface {
// l.SetLevel(Level(level))
return l
}
func (l *GormLogrus) Info(ctx context.Context, s string, args ...interface{}) {
l.WithContext(ctx)
l.Log(logrus.InfoLevel, fmt.Sprintf(s, args...))
// log.WithContext(ctx).Infof(s, args)
// log "github.com/sirupsen/logrus"
}
func (l *GormLogrus) Warn(ctx context.Context, s string, args ...interface{}) {
l.WithContext(ctx)
l.Log(logrus.WarnLevel, fmt.Sprintf(s, args...))
}
func (l *GormLogrus) Error(ctx context.Context, s string, args ...interface{}) {
l.WithContext(ctx)
l.Log(logrus.ErrorLevel, fmt.Sprintf(s, args...))
}
func (l *GormLogrus) Trace(ctx context.Context, begin time.Time, fc func() (string, int64), err error) {
elapsed := time.Since(begin)
sql, _ := fc()
fields := logrus.Fields{}
if l.SourceField != "" {
fields[l.SourceField] = utils.FileWithLineNum()
}
traceLevel := logrus.DebugLevel
if err != nil && !(errors.Is(err, gorm.ErrRecordNotFound) && l.SkipErrRecordNotFound) {
fields[logrus.ErrorKey] = err
traceLevel = logrus.ErrorLevel
}
if l.SlowThreshold != 0 && elapsed > l.SlowThreshold {
traceLevel = logrus.WarnLevel
}
l.WithContext(ctx).WithFields(fields)
l.Log(traceLevel, fmt.Sprintf("%s [%v]", sql, elapsed))
}
func (l *GormLogrus) SetSkipErrRecordNotFound(SkipErrRecordNotFound bool) gormlogger.Interface {
l.SkipErrRecordNotFound = SkipErrRecordNotFound
return l
}
func (l *GormLogrus) SetLogMode(level logrus.Level) gormlogger.Interface {
l.SetLevel(level)
return l
}