Skip to content

Commit

Permalink
Improve logging in postcli
Browse files Browse the repository at this point in the history
  • Loading branch information
poszu committed Jul 20, 2023
1 parent 391ff28 commit 30ca169
Showing 1 changed file with 27 additions and 5 deletions.
32 changes: 27 additions & 5 deletions cmd/postcli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

"github.com/davecgh/go-spew/spew"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"

"github.com/spacemeshos/post/config"
"github.com/spacemeshos/post/initialization"
Expand All @@ -38,9 +39,13 @@ var (
commitmentAtxIdHex string
commitmentAtxId []byte
reset bool

logLevel zapcore.Level
)

func parseFlags() {
flag.TextVar(&logLevel, "logLevel", zapcore.InfoLevel, "log level (debug, info, warn, error, dpanic, panic, fatal)")

flag.BoolVar(&printProviders, "printProviders", false, "print the list of compute providers")
flag.BoolVar(&printNumFiles, "printNumFiles", false, "print the total number of files that would be initialized")
flag.BoolVar(&printConfig, "printConfig", false, "print the used config and options")
Expand Down Expand Up @@ -121,15 +126,32 @@ func main() {
return
}

if err := processFlags(); err != nil {
log.Fatalln("failed to process flags", err)
}

zapLog, err := zap.NewProduction()
zapCfg := zap.Config{
Level: zap.NewAtomicLevelAt(logLevel),
Encoding: "console",
EncoderConfig: zapcore.EncoderConfig{
TimeKey: "T",
LevelKey: "L",
NameKey: "N",
MessageKey: "M",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: zapcore.CapitalLevelEncoder,
EncodeTime: zapcore.ISO8601TimeEncoder,
EncodeDuration: zapcore.StringDurationEncoder,
},
OutputPaths: []string{"stdout"},
ErrorOutputPaths: []string{"stderr"},
}

zapLog, err := zapCfg.Build()
if err != nil {
log.Fatalln("failed to initialize zap logger:", err)
}

if err := processFlags(); err != nil {
log.Fatalln("failed to process flags", err)
}

init, err := initialization.NewInitializer(
initialization.WithConfig(cfg),
initialization.WithInitOpts(opts),
Expand Down

0 comments on commit 30ca169

Please sign in to comment.