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
|
package ext
import (
"context"
"fmt"
"time"
"github.com/sirupsen/logrus"
"gorm.io/gorm/logger"
"gorm.io/gorm/utils"
)
type Log struct {
logrus *logrus.Entry
}
func getFullMsg(msg string, data ...interface{}) string {
return fmt.Sprintf(msg, append([]interface{}{utils.FileWithLineNum()}, data...)...)
}
func (self *Log) LogMode(_ logger.LogLevel) logger.Interface {
return self
}
func (self *Log) Info(ctx context.Context, msg string, data ...interface{}) {
fullMsg := getFullMsg(msg, data)
self.logrus.
WithContext(ctx).
Info(fullMsg)
}
func (self *Log) Warn(ctx context.Context, msg string, data ...interface{}) {
fullMsg := getFullMsg(msg, data)
self.logrus.
WithContext(ctx).
Warn(fullMsg)
}
func (self *Log) Error(ctx context.Context, msg string, data ...interface{}) {
fullMsg := getFullMsg(msg, data)
self.logrus.
WithContext(ctx).
Error(fullMsg)
}
func (self *Log) Trace(ctx context.Context, begin time.Time, fc func() (sql string, rowsAffected int64), _ error) {
elapsed := time.Since(begin)
sql, _ := fc()
self.logrus.
WithContext(ctx).
WithField("time", elapsed).
Printf(sql)
}
func Wraplog(log *logrus.Entry) *Log {
return &Log{
logrus: log,
}
}
|