From 9afd3953e670e06db2f4dd0d15907a3cbff70178 Mon Sep 17 00:00:00 2001 From: Florian Hoss Date: Thu, 26 Oct 2023 11:42:14 +0200 Subject: [PATCH] Replace zap with slog --- cmd/godash/godash.go | 16 ++++++------ go.mod | 3 +-- go.sum | 8 ++---- internal/bookmarks/bookmark.go | 34 +++++++++++++------------ internal/logger/logger.go | 25 +++++++++++++++++++ internal/logging/logger_test.go | 44 --------------------------------- internal/logging/logging.go | 33 ------------------------- internal/router/router.go | 7 +++++- internal/system/system.go | 4 +-- internal/weather/weather.go | 8 +++--- 10 files changed, 67 insertions(+), 115 deletions(-) create mode 100644 internal/logger/logger.go delete mode 100644 internal/logging/logger_test.go delete mode 100644 internal/logging/logging.go diff --git a/cmd/godash/godash.go b/cmd/godash/godash.go index ad2e4df..30e736c 100644 --- a/cmd/godash/godash.go +++ b/cmd/godash/godash.go @@ -2,29 +2,31 @@ package main import ( "fmt" - "log" + "log/slog" "net/http" + "os" "gitlab.unjx.de/flohoss/godash/internal/controller" "gitlab.unjx.de/flohoss/godash/internal/env" - "gitlab.unjx.de/flohoss/godash/internal/logging" + "gitlab.unjx.de/flohoss/godash/internal/logger" "gitlab.unjx.de/flohoss/godash/internal/router" - "go.uber.org/zap" ) func main() { env, err := env.Parse() if err != nil { - log.Fatal(err) + slog.Error("cannot parse environment variables", "err", err) + os.Exit(1) } - zap.ReplaceGlobals(logging.CreateLogger(env.LogLevel)) + slog.SetDefault(logger.NewLogger(env.LogLevel)) r := router.InitRouter() c := controller.NewController(env) router.SetupRoutes(r, c) - zap.L().Info("starting server", zap.String("url", fmt.Sprintf("http://localhost:%d", env.Port))) + slog.Info("starting server", "url", fmt.Sprintf("http://localhost:%d", env.Port)) if err := r.Start(fmt.Sprintf(":%d", env.Port)); err != http.ErrServerClosed { - zap.L().Fatal("cannot start server", zap.Error(err)) + slog.Error("cannot start server", "err", err) + os.Exit(1) } } diff --git a/go.mod b/go.mod index e36cc38..8864a66 100644 --- a/go.mod +++ b/go.mod @@ -9,11 +9,11 @@ require ( github.com/fsnotify/fsnotify v1.7.0 github.com/go-playground/validator/v10 v10.15.5 github.com/labstack/echo/v4 v4.11.2 + github.com/lmittmann/tint v1.0.2 github.com/r3labs/sse/v2 v2.10.0 github.com/shirou/gopsutil/v3 v3.23.9 github.com/stretchr/testify v1.8.4 github.com/unjx-de/go-folder v1.0.7 - go.uber.org/zap v1.26.0 gopkg.in/yaml.v3 v3.0.1 ) @@ -46,7 +46,6 @@ require ( github.com/valyala/bytebufferpool v1.0.0 // indirect github.com/valyala/fasttemplate v1.2.2 // indirect github.com/yusufpapurcu/wmi v1.2.3 // indirect - go.uber.org/multierr v1.11.0 // indirect golang.org/x/crypto v0.14.0 // indirect golang.org/x/net v0.17.0 // indirect golang.org/x/sys v0.13.0 // indirect diff --git a/go.sum b/go.sum index 5838e79..a83ec9a 100644 --- a/go.sum +++ b/go.sum @@ -54,6 +54,8 @@ github.com/labstack/gommon v0.4.0 h1:y7cvthEAEbU0yHOf4axH8ZG2NH8knB9iNSoTO8dyIk8 github.com/labstack/gommon v0.4.0/go.mod h1:uW6kP17uPlLJsD3ijUYn3/M5bAxtlZhMI6m3MFxTMTM= github.com/leodido/go-urn v1.2.4 h1:XlAE/cm/ms7TE/VMVoduSpNBoyc2dOxHs5MZSwAN63Q= github.com/leodido/go-urn v1.2.4/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNaVckg+4= +github.com/lmittmann/tint v1.0.2 h1:9XZ+JvEzjvd3VNVugYqo3j+dl0NRju8k9FquAusJExM= +github.com/lmittmann/tint v1.0.2/go.mod h1:HIS3gSy7qNwGCj+5oRjAutErFBl4BzdQP6cJZ0NfMwE= github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0/go.mod h1:zJYVVT2jmtg6P3p1VtQj7WsuWi/y4VnjVBn7F8KPB3I= github.com/lufia/plan9stats v0.0.0-20231016141302-07b5767bb0ed h1:036IscGBfJsFIgJQzlui7nK1Ncm0tp2ktmPj8xO4N/0= github.com/lufia/plan9stats v0.0.0-20231016141302-07b5767bb0ed/go.mod h1:ilwx/Dta8jXAgpFYFvSWEMwxmbWXyiUHkd5FwyKhb5k= @@ -116,12 +118,6 @@ github.com/valyala/fasttemplate v1.2.2/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+ github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= github.com/yusufpapurcu/wmi v1.2.3 h1:E1ctvB7uKFMOJw3fdOW32DwGE9I7t++CRUEMKvFoFiw= github.com/yusufpapurcu/wmi v1.2.3/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0= -go.uber.org/goleak v1.2.0 h1:xqgm/S+aQvhWFTtR0XK3Jvg7z8kGV8P4X14IzwN3Eqk= -go.uber.org/goleak v1.2.0/go.mod h1:XJYK+MuIchqpmGmUSAzotztawfKvYLUIgg7guXrwVUo= -go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0= -go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= -go.uber.org/zap v1.26.0 h1:sI7k6L95XOKS281NhVKOFCUNIvv9e0w4BF8N3u+tCRo= -go.uber.org/zap v1.26.0/go.mod h1:dtElttAiwGvoJ/vj4IwHBS/gXsEu/pZ50mUIRWuG0so= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.3.0/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4= diff --git a/internal/bookmarks/bookmark.go b/internal/bookmarks/bookmark.go index b5f2583..a89b847 100644 --- a/internal/bookmarks/bookmark.go +++ b/internal/bookmarks/bookmark.go @@ -2,12 +2,12 @@ package bookmarks import ( "io" + "log/slog" "os" "strings" "github.com/fsnotify/fsnotify" folderCreate "github.com/unjx-de/go-folder" - "go.uber.org/zap" "gopkg.in/yaml.v3" ) @@ -29,44 +29,45 @@ func (c *Config) createFolderStructure() { folders := []string{StorageDir, IconsDir} err := folderCreate.CreateFolders(folders, 0755) if err != nil { - zap.L().Fatal("cannot create folder", zap.Error(err)) + slog.Error("cannot create folder", "err", err) + os.Exit(1) } - zap.L().Debug("folders created", zap.Strings("folders", folders)) + slog.Debug("folders created", "folders", folders) } func (c *Config) copyDefaultConfigIfNotExisting() { _, err := os.Open(StorageDir + configFile) if err != nil { - zap.L().Debug(configFile + " not existing, creating...") + slog.Debug(configFile + " not existing, creating...") source, _ := os.Open(bookmarksFolder + configFile) defer source.Close() destination, err := os.Create(StorageDir + configFile) if err != nil { - zap.L().Error(err.Error()) + slog.Error(err.Error()) return } defer destination.Close() _, err = io.Copy(destination, source) if err != nil { - zap.L().Error(err.Error()) + slog.Error(err.Error()) return } - zap.L().Debug(configFile + " created") + slog.Debug(configFile + " created") } else { - zap.L().Debug(configFile + " existing, skipping creation") + slog.Debug(configFile + " existing, skipping creation") } } func (c *Config) readBookmarksFile() []byte { file, err := os.Open(StorageDir + configFile) if err != nil { - zap.L().Error(err.Error()) + slog.Error(err.Error()) return nil } defer file.Close() byteValue, err := io.ReadAll(file) if err != nil { - zap.L().Error(err.Error()) + slog.Error(err.Error()) return nil } return byteValue @@ -86,7 +87,7 @@ func (c *Config) parseBookmarks() { byteValue := c.readBookmarksFile() err := yaml.Unmarshal(byteValue, &c.Parsed) if err != nil { - zap.L().Error(err.Error()) + slog.Error(err.Error()) return } c.replaceIconString() @@ -95,7 +96,7 @@ func (c *Config) parseBookmarks() { func (c *Config) watchBookmarks() { watcher, err := fsnotify.NewWatcher() if err != nil { - zap.L().Error(err.Error()) + slog.Error(err.Error()) } defer watcher.Close() done := make(chan bool) @@ -103,17 +104,18 @@ func (c *Config) watchBookmarks() { go func() { for { select { - case _ = <-watcher.Events: + case <-watcher.Events: c.parseBookmarks() - zap.L().Debug("bookmarks changed", zap.Int("applications", len(c.Parsed.Applications)), zap.Int("links", len(c.Parsed.Links))) + slog.Debug("bookmarks changed", "applications", len(c.Parsed.Applications), "links", len(c.Parsed.Links)) case err := <-watcher.Errors: - zap.L().Error(err.Error()) + slog.Error(err.Error()) } } }() if err := watcher.Add(StorageDir + configFile); err != nil { - zap.L().Fatal("cannot add watcher") + slog.Error("cannot add watcher") + os.Exit(1) } <-done } diff --git a/internal/logger/logger.go b/internal/logger/logger.go new file mode 100644 index 0000000..7cc980c --- /dev/null +++ b/internal/logger/logger.go @@ -0,0 +1,25 @@ +package logger + +import ( + "log/slog" + "os" + "time" + + "github.com/lmittmann/tint" +) + +func NewLogger(logLevel string) *slog.Logger { + w := os.Stderr + + lvl := new(slog.Level) + level := slog.LevelInfo + err := lvl.UnmarshalText([]byte(logLevel)) + if err == nil { + level = *lvl + } + + return slog.New(tint.NewHandler(w, &tint.Options{ + Level: &level, + TimeFormat: time.Kitchen, + })) +} diff --git a/internal/logging/logger_test.go b/internal/logging/logger_test.go deleted file mode 100644 index 9933687..0000000 --- a/internal/logging/logger_test.go +++ /dev/null @@ -1,44 +0,0 @@ -package logging - -import ( - "fmt" - "testing" - - "github.com/stretchr/testify/assert" -) - -func TestInfoLogger(t *testing.T) { - level := "info" - log := CreateLogger(level) - defer log.Sync() - - assert.NotEmpty(t, log, "Logger should not be nil") - assert.Equal(t, log.Level().String(), level, fmt.Sprintf("Level should be %s", level)) -} - -func TestWarnLogger(t *testing.T) { - level := "warn" - log := CreateLogger(level) - defer log.Sync() - - assert.NotEmpty(t, log, "Logger should not be nil") - assert.Equal(t, log.Level().String(), level, fmt.Sprintf("Level should be %s", level)) -} - -func TestDebugLogger(t *testing.T) { - level := "debug" - log := CreateLogger(level) - defer log.Sync() - - assert.NotEmpty(t, log, "Logger should not be nil") - assert.Equal(t, log.Level().String(), level, fmt.Sprintf("Level should be %s", level)) -} - -func TestInvalidLogger(t *testing.T) { - level := "invalid" - log := CreateLogger(level) - defer log.Sync() - - assert.NotEmpty(t, log, "Logger should not be nil") - assert.Equal(t, log.Level().String(), "info", "Level should be info") -} diff --git a/internal/logging/logging.go b/internal/logging/logging.go deleted file mode 100644 index 81ea2cc..0000000 --- a/internal/logging/logging.go +++ /dev/null @@ -1,33 +0,0 @@ -package logging - -import ( - "time" - - "go.uber.org/zap" - "go.uber.org/zap/zapcore" -) - -func CreateLogger(logLevel string) *zap.Logger { - encoderCfg := zap.NewProductionEncoderConfig() - encoderCfg.TimeKey = "time" - encoderCfg.EncodeTime = zapcore.TimeEncoderOfLayout(time.StampMilli) - - level := zap.NewAtomicLevelAt(zap.InfoLevel) - zapLevel, err := zap.ParseAtomicLevel(logLevel) - if err == nil { - level = zapLevel - } - - config := zap.Config{ - Level: level, - Development: false, - DisableCaller: false, - DisableStacktrace: false, - Sampling: nil, - Encoding: "json", - EncoderConfig: encoderCfg, - OutputPaths: []string{"stdout"}, - ErrorOutputPaths: []string{"stderr"}, - } - return zap.Must(config.Build()) -} diff --git a/internal/router/router.go b/internal/router/router.go index 7068cf3..a47882e 100644 --- a/internal/router/router.go +++ b/internal/router/router.go @@ -2,6 +2,7 @@ package router import ( "net/http" + "strings" "github.com/labstack/echo/v4" "github.com/labstack/echo/v4/middleware" @@ -16,7 +17,11 @@ func InitRouter() *echo.Echo { e.Pre(middleware.RemoveTrailingSlash()) e.Use(middleware.Recover()) - e.Use(middleware.Gzip()) + e.Use(middleware.GzipWithConfig(middleware.GzipConfig{ + Skipper: func(c echo.Context) bool { + return strings.Contains(c.Path(), "sse") + }, + })) e.Renderer = initTemplates() diff --git a/internal/system/system.go b/internal/system/system.go index 0f6aa3f..8fa24c0 100644 --- a/internal/system/system.go +++ b/internal/system/system.go @@ -2,10 +2,10 @@ package system import ( "encoding/json" + "log/slog" "time" "github.com/r3labs/sse/v2" - "go.uber.org/zap" ) func NewSystemService(enabled bool, sse *sse.Server) *System { @@ -35,5 +35,5 @@ func (c *Config) Initialize() { c.System.Static.Ram = staticRam() c.System.Static.Disk = staticDisk() go c.UpdateLiveInformation() - zap.L().Debug("system updated", zap.String("cpu", c.System.Static.CPU.Name), zap.String("arch", c.System.Static.Host.Architecture)) + slog.Debug("system updated", "cpu", c.System.Static.CPU.Name, "arch", c.System.Static.Host.Architecture) } diff --git a/internal/weather/weather.go b/internal/weather/weather.go index 993c5a1..8e4fcb7 100644 --- a/internal/weather/weather.go +++ b/internal/weather/weather.go @@ -4,13 +4,13 @@ import ( "encoding/json" "fmt" "io" + "log/slog" "math" "net/http" "time" "github.com/caarlos0/env/v8" "github.com/r3labs/sse/v2" - "go.uber.org/zap" ) func NewWeatherService(sse *sse.Server) *Weather { @@ -58,15 +58,15 @@ func (w *Weather) updateWeather(interval time.Duration) { w.config.Units, w.config.Lang)) if err != nil || resp.StatusCode != 200 { - zap.L().Error("weather cannot be updated, please check WEATHER_KEY") + slog.Error("weather cannot be updated, please check WEATHER_KEY") } else { body, _ := io.ReadAll(resp.Body) err = json.Unmarshal(body, &weatherResponse) if err != nil { - zap.L().Error("weather cannot be processed") + slog.Error("weather cannot be processed") } else { w.copyWeatherValues(&weatherResponse) - zap.L().Debug("weather updated", zap.Float64("temp", w.CurrentWeather.Temp)) + slog.Debug("weather updated", "temp", w.CurrentWeather.Temp) } resp.Body.Close() json, _ := json.Marshal(w.CurrentWeather)