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 --- cmd/server/main.go | 46 +++++++++++++++++++++++++++++++--------------- 1 file changed, 31 insertions(+), 15 deletions(-) (limited to 'cmd') diff --git a/cmd/server/main.go b/cmd/server/main.go index 41b2b4a..c2160c4 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -4,6 +4,7 @@ import ( "context" "encoding/base64" "errors" + "log/slog" "net/http" "os" "os/signal" @@ -11,7 +12,6 @@ import ( "github.com/glebarez/sqlite" "github.com/gorilla/mux" - "github.com/sirupsen/logrus" flag "github.com/spf13/pflag" "gorm.io/driver/mysql" "gorm.io/driver/postgres" @@ -33,19 +33,20 @@ func main() { key = flag.String("aes-key", "", "AES key, either 16, 24, or 32 bytes string to select AES-128, AES-192, or AES-256") dbType = flag.String("db-type", "sqlite", "Database to be used. Choose either mysql, psql or sqlite") dbCon = flag.String("db-con", "main.db", "Database string connection for given database type. Ref: https://gorm.io/docs/connecting_to_the_database.html") - logLevel = flag.String("log-level", "error", "Log level: Choose either trace, debug, info, warning, error, fatal or panic") + logLevel = flag.String("log-level", "error", "Log level: Choose either debug, info, warning, error") schedulerCount = flag.Uint("scheduler-count", 10, "How many workers are created to process media files") cachePath = flag.String("cache-path", "", "Folder to store thumbnail image") ) flag.Parse() - l, err := logrus.ParseLevel(*logLevel) - if err != nil { - panic("failed to parse log level" + err.Error()) - } - logger := logrus.New() - logger.SetLevel(l) + level := parseLogLevel(*logLevel) + + handler := slog.NewTextHandler( + os.Stdout, + &slog.HandlerOptions{Level: level}, + ) + logger := slog.New(handler) d, err := OpenDatabase(*dbType, *dbCon) if err != nil { @@ -53,7 +54,7 @@ func main() { } db, err := gorm.Open(d, &gorm.Config{ - Logger: ext.Wraplog(logger.WithField("context", "sql")), + Logger: ext.Wraplog(logger.With("context", "sql")), }) if err != nil { panic("failed to connect database: " + err.Error()) @@ -85,8 +86,8 @@ func main() { // middleware var ( - authMiddleware = ext.NewAuthMiddleware(baseKey, logger.WithField("context", "auth"), userRepository) - logMiddleware = ext.NewLogMiddleare(logger.WithField("context", "http")) + authMiddleware = ext.NewAuthMiddleware(baseKey, logger.With("context", "auth"), userRepository) + logMiddleware = ext.NewLogMiddleare(logger.With("context", "http")) initialMiddleware = ext.NewInitialSetupMiddleware(userRepository) ) @@ -129,22 +130,22 @@ func main() { fileTask = worker.NewTaskFromChanProcessor[string]( fileScanner, scheduler, - logrus.WithField("context", "file scanner"), + logger.With("context", "file scanner"), ) exifTask = worker.NewTaskFromBatchProcessor[*repository.Media]( exifScanner, scheduler, - logrus.WithField("context", "exif scanner"), + logger.With("context", "exif scanner"), ) thumbnailTask = worker.NewTaskFromBatchProcessor[*repository.Media]( thumbnailScanner, scheduler, - logrus.WithField("context", "thumbnail scanner"), + logger.With("context", "thumbnail scanner"), ) albumTask = worker.NewTaskFromSerialProcessor[*repository.Media]( albumScanner, scheduler, - logrus.WithField("context", "thumbnail scanner"), + logger.With("context", "thumbnail scanner"), ) ) @@ -173,3 +174,18 @@ func OpenDatabase(dbType string, dbConn string) (gorm.Dialector, error) { return nil, errors.New("No valid db type given") } } + +func parseLogLevel(input string) slog.Level { + switch input { + case "debug": + return slog.LevelDebug + case "info": + return slog.LevelInfo + case "warn": + return slog.LevelWarn + case "error": + return slog.LevelError + default: + return slog.LevelWarn + } +} -- cgit v1.2.3