lens @ 398ae343e6a33fb81f82380b17081b8f9bae328a

 1package ext
 2
 3import (
 4	"context"
 5	"fmt"
 6	"time"
 7
 8	"github.com/sirupsen/logrus"
 9	"gorm.io/gorm/logger"
10	"gorm.io/gorm/utils"
11)
12
13type Log struct {
14	logrus *logrus.Entry
15}
16
17func getFullMsg(msg string, data ...interface{}) string {
18	return fmt.Sprintf(msg, append([]interface{}{utils.FileWithLineNum()}, data...)...)
19}
20
21func (self *Log) LogMode(_ logger.LogLevel) logger.Interface {
22	return self
23}
24
25func (self *Log) Info(ctx context.Context, msg string, data ...interface{}) {
26	fullMsg := getFullMsg(msg, data)
27	self.logrus.
28		WithContext(ctx).
29		Info(fullMsg)
30}
31
32func (self *Log) Warn(ctx context.Context, msg string, data ...interface{}) {
33	fullMsg := getFullMsg(msg, data)
34	self.logrus.
35		WithContext(ctx).
36		Warn(fullMsg)
37}
38func (self *Log) Error(ctx context.Context, msg string, data ...interface{}) {
39	fullMsg := getFullMsg(msg, data)
40	self.logrus.
41		WithContext(ctx).
42		Error(fullMsg)
43}
44
45func (self *Log) Trace(ctx context.Context, begin time.Time, fc func() (sql string, rowsAffected int64), _ error) {
46	elapsed := time.Since(begin)
47	sql, _ := fc()
48	self.logrus.
49		WithContext(ctx).
50		WithField("time", elapsed).
51		Printf(sql)
52}
53
54func Wraplog(log *logrus.Entry) *Log {
55	return &Log{
56		logrus: log,
57	}
58}