feat: adjustable log level

This commit is contained in:
Yiyang Kang 2023-03-08 04:46:00 +08:00
parent ed46c2b274
commit 89ddf89052
3 changed files with 13 additions and 2 deletions

View File

@ -49,7 +49,6 @@ func handleTranslateCmd(c tele.Context) error {
tele.Silent, tele.Silent,
) )
} }
logger.Infof("trimmed message: %q", payload)
_, err := c.Bot().Reply(msg, "Sure. To what language?", tele.Silent, translateMenu) _, err := c.Bot().Reply(msg, "Sure. To what language?", tele.Silent, translateMenu)
return err return err

1
cfg.go
View File

@ -10,6 +10,7 @@ import (
) )
type ConfigDef struct { type ConfigDef struct {
LogLevel string `env:"TG_LOG_LEVEL" env-default:"info"`
AdminUIDs []int64 `env:"TG_ADMIN_UIDS"` AdminUIDs []int64 `env:"TG_ADMIN_UIDS"`
TGBotToken string `env:"TG_TOKEN" env-required:""` TGBotToken string `env:"TG_TOKEN" env-required:""`
TGAnnounceCommands bool `env:"TG_ANNOUNCE_CMDS"` TGAnnounceCommands bool `env:"TG_ANNOUNCE_CMDS"`

13
main.go
View File

@ -7,6 +7,7 @@ import (
"time" "time"
"go.uber.org/zap" "go.uber.org/zap"
"go.uber.org/zap/zapcore"
tele "gopkg.in/telebot.v3" tele "gopkg.in/telebot.v3"
"git.gensokyo.cafe/kkyy/mycurrencynet" "git.gensokyo.cafe/kkyy/mycurrencynet"
@ -14,9 +15,13 @@ import (
) )
var logger *zap.SugaredLogger var logger *zap.SugaredLogger
var loglvl zap.AtomicLevel
func initLogger() { func initLogger() {
l, err := zap.NewProduction() logCfg := zap.NewProductionConfig()
loglvl = logCfg.Level
l, err := logCfg.Build()
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -89,6 +94,12 @@ func main() {
if err := LoadCfg(); err != nil { if err := LoadCfg(); err != nil {
logger.Fatalw("Failed to load config", "err", err) 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() runBot()
} }