diff options
| author | Jeff Halter <868228+jhalter@users.noreply.github.com> | 2023-11-20 13:40:14 -0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-11-20 13:40:14 -0800 |
| commit | ea2027a385e006ac5186e38f7f391dc20df338e7 (patch) | |
| tree | 7750973ff6b7e1a2063677e863bb3962d467ad00 /cmd/mobius-hotline-client/main.go | |
| parent | 6e4c02b1b9793776a52cf8b1f914eeadd2bbb4b5 (diff) | |
Refactor client to use slog and pass context (#107)
* Refactor client to use slog and pass context
* Bump CI golang version
Diffstat (limited to 'cmd/mobius-hotline-client/main.go')
| -rw-r--r-- | cmd/mobius-hotline-client/main.go | 52 |
1 files changed, 12 insertions, 40 deletions
diff --git a/cmd/mobius-hotline-client/main.go b/cmd/mobius-hotline-client/main.go index 72b581b..20bd7f2 100644 --- a/cmd/mobius-hotline-client/main.go +++ b/cmd/mobius-hotline-client/main.go @@ -6,15 +6,20 @@ import ( "fmt" "github.com/jhalter/mobius/hotline" "github.com/rivo/tview" - "go.uber.org/zap" - "go.uber.org/zap/zapcore" - "log" + "log/slog" "os" "os/signal" "runtime" "syscall" ) +var logLevels = map[string]slog.Level{ + "debug": slog.LevelDebug, + "info": slog.LevelInfo, + "warn": slog.LevelWarn, + "error": slog.LevelError, +} + func main() { _, cancelRoot := context.WithCancel(context.Background()) @@ -33,41 +38,27 @@ func main() { os.Exit(0) } - zapLvl, ok := zapLogLevel[*logLevel] - if !ok { - fmt.Printf("Invalid log level %s. Must be debug, info, warn, or error.\n", *logLevel) - os.Exit(0) - } - // init DebugBuffer db := &hotline.DebugBuffer{ TextView: tview.NewTextView(), } - cores := []zapcore.Core{newZapCore(zapLvl, db)} - // Add file logger if optional log-file flag was passed if *logFile != "" { f, err := os.OpenFile(*logFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) - if err != nil { - log.Println(err) - } - defer f.Close() + defer func() { _ = f.Close() }() if err != nil { panic(err) } - cores = append(cores, newZapCore(zapLvl, f)) } - l := zap.New(zapcore.NewTee(cores...)) - defer func() { _ = l.Sync() }() - logger := l.Sugar() - logger.Infow("Started Mobius client", "Version", hotline.VERSION) + logger := slog.New(slog.NewTextHandler(db, &slog.HandlerOptions{Level: logLevels[*logLevel]})) + logger.Info("Started Mobius client", "Version", hotline.VERSION) go func() { sig := <-sigChan - logger.Infow("Stopping client", "signal", sig.String()) + logger.Info("Stopping client", "signal", sig.String()) cancelRoot() }() @@ -76,25 +67,6 @@ func main() { client.UI.Start() } -func newZapCore(level zapcore.Level, syncer zapcore.WriteSyncer) zapcore.Core { - encoderCfg := zap.NewProductionEncoderConfig() - encoderCfg.TimeKey = "timestamp" - encoderCfg.EncodeTime = zapcore.ISO8601TimeEncoder - - return zapcore.NewCore( - zapcore.NewConsoleEncoder(encoderCfg), - zapcore.Lock(syncer), - level, - ) -} - -var zapLogLevel = map[string]zapcore.Level{ - "debug": zap.DebugLevel, - "info": zap.InfoLevel, - "warn": zap.WarnLevel, - "error": zap.ErrorLevel, -} - func defaultConfigPath() (cfgPath string) { switch runtime.GOOS { case "windows": |