From 7a1c164d16461fc7684a49259271ba463e7e4156 Mon Sep 17 00:00:00 2001 From: Nate Maninger Date: Mon, 9 Oct 2023 11:52:49 -0600 Subject: [PATCH] cmd: support changing log level --- cmd/fsd/main.go | 21 +++++++++++++++++++++ config/config.go | 5 +++++ 2 files changed, 26 insertions(+) diff --git a/cmd/fsd/main.go b/cmd/fsd/main.go index 9d3b63c..2f05f38 100644 --- a/cmd/fsd/main.go +++ b/cmd/fsd/main.go @@ -40,6 +40,9 @@ var ( API: config.API{ Address: ":8081", }, + Log: config.Log{ + Level: "info", + }, } ) @@ -91,6 +94,24 @@ func main() { mustLoadConfig(dir, log) + var level zap.AtomicLevel + switch cfg.Log.Level { + case "debug": + level = zap.NewAtomicLevelAt(zap.DebugLevel) + case "info": + level = zap.NewAtomicLevelAt(zap.InfoLevel) + case "warn": + level = zap.NewAtomicLevelAt(zap.WarnLevel) + case "error": + level = zap.NewAtomicLevelAt(zap.ErrorLevel) + default: + log.Fatal("invalid log level", zap.String("level", cfg.Log.Level)) + } + + log = log.WithOptions(zap.WrapCore(func(core zapcore.Core) zapcore.Core { + return zapcore.NewCore(consoleEncoder, zapcore.Lock(os.Stdout), level) + })) + ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM, syscall.SIGINT) defer cancel() diff --git a/config/config.go b/config/config.go index 58e7207..d7c85ed 100644 --- a/config/config.go +++ b/config/config.go @@ -23,10 +23,15 @@ type ( Password string `yaml:"password"` } + Log struct { + Level string `yaml:"level"` + } + // Config contains the configuration for fsd Config struct { Renterd Renterd `yaml:"renterd"` IPFS IPFS `yaml:"ipfs"` API API `yaml:"api"` + Log Log `yaml:"log"` } )