package ext import ( "context" "fmt" "log/slog" "time" "gorm.io/gorm/logger" "gorm.io/gorm/utils" ) type Log struct { logger *slog.Logger } 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.logger.InfoContext(ctx, fullMsg) } func (self *Log) Warn(ctx context.Context, msg string, data ...interface{}) { fullMsg := getFullMsg(msg, data) self.logger. WarnContext(ctx, fullMsg) } func (self *Log) Error(ctx context.Context, msg string, data ...interface{}) { fullMsg := getFullMsg(msg, data) self.logger. ErrorContext(ctx, 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.logger. InfoContext(ctx, sql, slog.Duration("elapsed", elapsed)) } func Wraplog(log *slog.Logger) *Log { return &Log{ logger: log, } }