forked from openflagr/flagr
/
logger.go
43 lines (35 loc) · 1.05 KB
/
logger.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
package entity
import (
"context"
"time"
logrus "github.com/sirupsen/logrus"
gorm_logger "gorm.io/gorm/logger"
)
type Logger struct {
LogLevel gorm_logger.LogLevel
SlowThreshold time.Duration
IgnoreRecordNotFoundError bool
}
func (l *Logger) LogMode(level gorm_logger.LogLevel) gorm_logger.Interface {
newlogger := *l
newlogger.LogLevel = level
return &newlogger
}
func (l *Logger) Info(ctx context.Context, s string, args ...interface{}) {
logrus.WithContext(ctx).Infof(s, args...)
}
func (l *Logger) Warn(ctx context.Context, s string, args ...interface{}) {
logrus.WithContext(ctx).Warnf(s, args...)
}
func (l *Logger) Error(ctx context.Context, s string, args ...interface{}) {
logrus.WithContext(ctx).Errorf(s, args...)
}
func (l *Logger) Trace(ctx context.Context, begin time.Time, fc func() (string, int64), err error) {
if logrus.GetLevel() >= logrus.TraceLevel {
sql, _ := fc()
elapsed := time.Since(begin)
if elapsed > l.SlowThreshold {
logrus.WithContext(ctx).Tracef("%s [%s]", sql, elapsed)
}
}
}