Skip to content

Commit

Permalink
chore(log): Migrate to slog
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Feb 29, 2024
1 parent e094171 commit 006149f
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 19 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ require (
github.com/knadh/koanf/v2 v2.1.0
github.com/ncruces/zenity v0.10.11
github.com/pelletier/go-toml/v2 v2.1.0
github.com/sirupsen/logrus v1.9.3
github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966
github.com/spf13/pflag v1.0.5
)
Expand Down
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRI
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/randall77/makefat v0.0.0-20210315173500-7ddd0e42c844 h1:GranzK4hv1/pqTIhMTXt2X8MmMOuH3hMeUR0o9SP5yc=
github.com/randall77/makefat v0.0.0-20210315173500-7ddd0e42c844/go.mod h1:T1TLSfyWVBRXVGzWd0o9BI4kfoO9InEgfQe4NV3mLz8=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966 h1:JIAuq3EEf9cgbU6AtGPK4CTG3Zf6CKMNqf0MHTggAUA=
github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966/go.mod h1:sUM3LWHvSMaG192sy56D9F7CNvL7jUJVXoqM1QKLnog=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
Expand All @@ -55,7 +53,6 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
Expand All @@ -66,7 +63,6 @@ go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
golang.org/x/image v0.15.0 h1:kOELfmgrmJlw4Cdb7g/QGuB3CvDrXbqEIww/pNtNBm8=
golang.org/x/image v0.15.0/go.mod h1:HUYqC05R2ZcZ3ejNQsIHQDQiwWM4JBqmm6MKANTp4LE=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
14 changes: 7 additions & 7 deletions internal/config/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package config
import (
"bytes"
"errors"
"log/slog"
"os"
"path/filepath"
"time"
Expand All @@ -14,7 +15,6 @@ import (
"github.com/knadh/koanf/providers/structs"
"github.com/knadh/koanf/v2"
"github.com/pelletier/go-toml/v2"
log "github.com/sirupsen/logrus"
flag "github.com/spf13/pflag"
)

Expand Down Expand Up @@ -68,7 +68,7 @@ func Load() error {
return err
}

log.WithField("file", cfgFile).Info("Loaded config")
slog.Info("Loaded config", "file", cfgFile)
return err
}

Expand Down Expand Up @@ -101,13 +101,13 @@ func Write() error {

if !bytes.Equal(cfgContents, newCfg) {
if cfgNotExists {
log.WithField("file", cfgFile).Info("Creating config")
slog.Info("Creating config", "file", cfgFile)

if err := os.MkdirAll(filepath.Dir(cfgFile), 0o777); err != nil {
return err
}
} else {
log.WithField("file", cfgFile).Info("Updating config")
slog.Info("Updating config", "file", cfgFile)
}

if err := os.WriteFile(cfgFile, newCfg, 0o666); err != nil {
Expand All @@ -119,19 +119,19 @@ func Write() error {
}

func Watch() error {
log.WithField("file", cfgFile).Info("Watching config")
slog.Info("Watching config", "file", cfgFile)
f := file.Provider(cfgFile)
return f.Watch(func(event interface{}, err error) {
if err != nil {
log.WithError(err).Error("Config watcher failed")
slog.Error("Config watcher failed", "error", err.Error())
time.Sleep(time.Second)
defer func() {
_ = Watch()
}()
}

if err := Load(); err != nil {
log.WithError(err).Error("Failed to load config")
slog.Error("Failed to load config", "error", err.Error())
}
for _, reloader := range reloaders {
reloader()
Expand Down
4 changes: 2 additions & 2 deletions internal/local_file/local_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package local_file

import (
"errors"
"log"
"log/slog"
"os"
"strconv"
"strings"
Expand Down Expand Up @@ -36,7 +36,7 @@ func ReloadConfig() {
}
if newPath != path {
if err := Cleanup(); err != nil {
log.Println(err)
slog.Error("Failed to cleanup local file", "error", err.Error())
}
}
path = newPath
Expand Down
4 changes: 2 additions & 2 deletions internal/ticker/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package ticker

import (
"errors"
"log"
"log/slog"
"time"

"github.com/gabe565/nightscout-menu-bar/internal/config"
Expand Down Expand Up @@ -32,7 +32,7 @@ func Fetch() {
RenderCh <- properties
if config.Default.LocalFile.Enabled {
if err := local_file.Write(properties); err != nil {
log.Println(err)
slog.Error("Failed to write local file", "error", err.Error())
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions internal/tray/systray.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package tray

import (
"log"
"log/slog"

"fyne.io/systray"
"github.com/gabe565/nightscout-menu-bar/internal/assets"
Expand Down Expand Up @@ -121,8 +121,8 @@ func onReady() {
}

func onExit() {
log.Println("exiting")
slog.Info("Exiting")
if err := local_file.Cleanup(); err != nil {
log.Println(err)
slog.Error("Failed to cleanup local file", "error", err.Error())
}
}

0 comments on commit 006149f

Please sign in to comment.