diff options
Diffstat (limited to 'pkg/ext')
-rw-r--r-- | pkg/ext/gorm_logger.go | 28 | ||||
-rw-r--r-- | pkg/ext/middleware.go | 43 |
2 files changed, 34 insertions, 37 deletions
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, } } diff --git a/pkg/ext/middleware.go b/pkg/ext/middleware.go index 6a94c4f..38bacca 100644 --- a/pkg/ext/middleware.go +++ b/pkg/ext/middleware.go @@ -4,11 +4,10 @@ import ( "context" "encoding/base64" "errors" + "log/slog" "net/http" "time" - "github.com/sirupsen/logrus" - "git.sr.ht/~gabrielgio/img/pkg/database/repository" "git.sr.ht/~gabrielgio/img/pkg/service" ) @@ -24,7 +23,7 @@ type ( User string LogMiddleware struct { - entry *logrus.Entry + logger *slog.Logger } ) @@ -32,9 +31,9 @@ const ( UserKey User = "user" ) -func NewLogMiddleare(log *logrus.Entry) *LogMiddleware { +func NewLogMiddleare(log *slog.Logger) *LogMiddleware { return &LogMiddleware{ - entry: log, + logger: log, } } @@ -43,27 +42,29 @@ func (l *LogMiddleware) HTTP(next http.HandlerFunc) http.HandlerFunc { start := time.Now() next(w, r) elapsed := time.Since(start) - l.entry. - WithField("time", elapsed). - WithField("path", r.URL.Path). - Info(r.Method) + l.logger.Info( + r.Method, + slog.Duration("elapsed", elapsed), + slog.String("path", r.URL.Path), + ) + } } type AuthMiddleware struct { key []byte - entry *logrus.Entry + logger *slog.Logger userRepository repository.UserRepository } func NewAuthMiddleware( key []byte, - log *logrus.Entry, + logger *slog.Logger, userRepository repository.UserRepository, ) *AuthMiddleware { return &AuthMiddleware{ key: key, - entry: log.WithField("context", "auth"), + logger: logger, userRepository: userRepository, } } @@ -79,35 +80,37 @@ func (a *AuthMiddleware) LoggedIn(next http.HandlerFunc) http.HandlerFunc { redirectLogin := "/login?redirect=" + path authBase64, err := r.Cookie("auth") if errors.Is(err, http.ErrNoCookie) { - a.entry.Info("No auth provided") + a.logger.Info("No auth provided") http.Redirect(w, r, redirectLogin, http.StatusTemporaryRedirect) return } auth, err := base64.StdEncoding.DecodeString(authBase64.Value) if err != nil { - a.entry.Error(err) + a.logger.Error(err.Error()) return } token, err := service.ReadToken(auth, a.key) if err != nil { - a.entry.Error(err) + a.logger.Error(err.Error()) http.Redirect(w, r, redirectLogin, http.StatusTemporaryRedirect) return } user, err := a.userRepository.Get(r.Context(), token.UserID) if err != nil { - a.entry.Error(err) + a.logger.Error(err.Error()) return } r = r.WithContext(context.WithValue(r.Context(), UserKey, user)) - a.entry. - WithField("userID", token.UserID). - WithField("username", token.Username). - Info("user recognized") + a.logger. + Info( + "user recognized", + slog.Uint64("userid", uint64(token.UserID)), + slog.String("username", token.Username), + ) next(w, r) } } |