From 668620a09037888c598b99444d783434655c3dea Mon Sep 17 00:00:00 2001 From: Ash Keel Date: Thu, 24 Nov 2022 13:15:59 +0100 Subject: [PATCH] feat: add log-level flag --- logging.go | 8 ++++---- main.go | 9 ++++++++- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/logging.go b/logging.go index dceb5df..58c2ff5 100644 --- a/logging.go +++ b/logging.go @@ -18,14 +18,14 @@ var ( incomingLogs chan LogEntry ) -func initLogger() { +func initLogger(level zapcore.Level) { lastLogs = make([]LogEntry, 0) incomingLogs = make(chan LogEntry, 100) - logStorage := NewLogStorage(zap.InfoLevel) + logStorage := NewLogStorage(level) consoleLogger := zapcore.NewCore( zapcore.NewConsoleEncoder(zap.NewDevelopmentEncoderConfig()), zapcore.Lock(os.Stderr), - zap.InfoLevel, + level, ) fileLogger := zapcore.NewCore( zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig()), @@ -35,7 +35,7 @@ func initLogger() { MaxBackups: 3, MaxAge: 28, }), - zap.DebugLevel, + level, ) core := zapcore.NewTee( consoleLogger, diff --git a/main.go b/main.go index 1d9938c..9803ad7 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,8 @@ import ( "os" "time" + "go.uber.org/zap/zapcore" + "github.com/strimertul/strimertul/modules" "github.com/strimertul/strimertul/modules/loyalty" "github.com/strimertul/strimertul/modules/twitch" @@ -45,6 +47,7 @@ func main() { Version: appVersion, Action: cliMain, Flags: []cli.Flag{ + &cli.StringFlag{Name: "log-level", Usage: "logging level (debug,info,warn,error)", Value: "info"}, &cli.StringFlag{Name: "driver", Usage: "specify database driver", Value: "auto"}, &cli.StringFlag{Name: "database-dir", Aliases: []string{"db-dir"}, Usage: "specify database directory", Value: "data"}, &cli.StringFlag{Name: "backup-dir", Aliases: []string{"b-dir"}, Usage: "specify backup directory", Value: "backups"}, @@ -85,7 +88,11 @@ func main() { rand.Seed(time.Now().UnixNano()) // Initialize logger with global flags - initLogger() + level, err := zapcore.ParseLevel(ctx.String("log-level")) + if err != nil { + level = zapcore.InfoLevel + } + initLogger(level) return nil }, After: func(ctx *cli.Context) error {