From c3ea735c0f03a0827a8e753a5b5adf6e31f4c925 Mon Sep 17 00:00:00 2001 From: Gabriel Arakaki Giovanini Date: Tue, 24 Oct 2023 19:12:52 +0200 Subject: feat: Migrate from logrus to slog --- pkg/ext/gorm_logger.go | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) (limited to 'pkg/ext/gorm_logger.go') diff --git a/pkg/ext/gorm_logger.go b/pkg/ext/gorm_logger.go index bfbbb1e..f0ab592 100644 --- a/pkg/ext/gorm_logger.go +++ b/pkg/ext/gorm_logger.go @@ -3,15 +3,15 @@ package ext import ( "context" "fmt" + "log/slog" "time" - "github.com/sirupsen/logrus" "gorm.io/gorm/logger" "gorm.io/gorm/utils" ) type Log struct { - logrus *logrus.Entry + logger *slog.Logger } func getFullMsg(msg string, data ...interface{}) string { @@ -24,35 +24,29 @@ func (self *Log) LogMode(_ logger.LogLevel) logger.Interface { func (self *Log) Info(ctx context.Context, msg string, data ...interface{}) { fullMsg := getFullMsg(msg, data) - self.logrus. - WithContext(ctx). - Info(fullMsg) + self.logger.InfoContext(ctx, fullMsg) } func (self *Log) Warn(ctx context.Context, msg string, data ...interface{}) { fullMsg := getFullMsg(msg, data) - self.logrus. - WithContext(ctx). - Warn(fullMsg) + self.logger. + WarnContext(ctx, fullMsg) } func (self *Log) Error(ctx context.Context, msg string, data ...interface{}) { fullMsg := getFullMsg(msg, data) - self.logrus. - WithContext(ctx). - Error(fullMsg) + 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.logrus. - WithContext(ctx). - WithField("time", elapsed). - Printf(sql) + self.logger. + InfoContext(ctx, sql, slog.Duration("elapsed", elapsed)) } -func Wraplog(log *logrus.Entry) *Log { +func Wraplog(log *slog.Logger) *Log { return &Log{ - logrus: log, + logger: log, } } -- cgit v1.2.3