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, } }