Skip to content

Commit

Permalink
🗜️ cmd: fix log level panic
Browse files Browse the repository at this point in the history
TextVar calls the MarshalText method on the default value, which panics when the AtomicLevel is zero value.

Switch to zapcore.Level and use InvalidLevel as the default value.
  • Loading branch information
database64128 committed Nov 21, 2023
1 parent 727c690 commit 239e110
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions cmd/cubic-rce-bot/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,21 @@ import (
"github.com/database64128/cubic-rce-bot/jsonhelper"
"github.com/database64128/cubic-rce-bot/logging"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)

var (
testConf bool
confPath string
zapConf string
logLevel zap.AtomicLevel
logLevel zapcore.Level
)

func init() {
flag.BoolVar(&testConf, "testConf", false, "Test the configuration file without starting the bot")
flag.StringVar(&confPath, "confPath", "", "Path to JSON configuration file")
flag.StringVar(&zapConf, "zapConf", "", "Preset name or path to JSON configuration file for building the zap logger.\nAvailable presets: console (default), systemd, production, development")
flag.TextVar(&logLevel, "logLevel", zap.AtomicLevel{}, "Override the logger configuration's log level.\nAvailable levels: debug, info, warn, error, dpanic, panic, fatal")
flag.TextVar(&logLevel, "logLevel", zapcore.InvalidLevel, "Override the logger configuration's log level.\nAvailable levels: debug, info, warn, error, dpanic, panic, fatal")
}

func main() {
Expand Down Expand Up @@ -55,8 +56,8 @@ func main() {
}
}

if logLevel != (zap.AtomicLevel{}) {
zc.Level = logLevel
if logLevel != zapcore.InvalidLevel {
zc.Level.SetLevel(logLevel)
}

logger, err := zc.Build()
Expand Down

0 comments on commit 239e110

Please sign in to comment.