From 89ddf89052f4f72af2331cbec99e61c786e30b76 Mon Sep 17 00:00:00 2001 From: Yiyang Kang Date: Wed, 8 Mar 2023 04:46:00 +0800 Subject: [PATCH] feat: adjustable log level --- botcmd_translate.go | 1 - cfg.go | 1 + main.go | 13 ++++++++++++- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/botcmd_translate.go b/botcmd_translate.go index 5c45e89..e83e49f 100644 --- a/botcmd_translate.go +++ b/botcmd_translate.go @@ -49,7 +49,6 @@ func handleTranslateCmd(c tele.Context) error { tele.Silent, ) } - logger.Infof("trimmed message: %q", payload) _, err := c.Bot().Reply(msg, "Sure. To what language?", tele.Silent, translateMenu) return err diff --git a/cfg.go b/cfg.go index 15a34e4..ce9efca 100644 --- a/cfg.go +++ b/cfg.go @@ -10,6 +10,7 @@ import ( ) type ConfigDef struct { + LogLevel string `env:"TG_LOG_LEVEL" env-default:"info"` AdminUIDs []int64 `env:"TG_ADMIN_UIDS"` TGBotToken string `env:"TG_TOKEN" env-required:""` TGAnnounceCommands bool `env:"TG_ANNOUNCE_CMDS"` diff --git a/main.go b/main.go index d8fab53..94df370 100644 --- a/main.go +++ b/main.go @@ -7,6 +7,7 @@ import ( "time" "go.uber.org/zap" + "go.uber.org/zap/zapcore" tele "gopkg.in/telebot.v3" "git.gensokyo.cafe/kkyy/mycurrencynet" @@ -14,9 +15,13 @@ import ( ) var logger *zap.SugaredLogger +var loglvl zap.AtomicLevel func initLogger() { - l, err := zap.NewProduction() + logCfg := zap.NewProductionConfig() + loglvl = logCfg.Level + + l, err := logCfg.Build() if err != nil { panic(err) } @@ -89,6 +94,12 @@ func main() { if err := LoadCfg(); err != nil { logger.Fatalw("Failed to load config", "err", err) } + parsedLvl, err := zapcore.ParseLevel(config.LogLevel) + if err != nil { + logger.Warnf("Invalid log level %q", config.LogLevel) + } else { + loglvl.SetLevel(parsedLvl) + } runBot() }