From 715fc8d21c145473a00c5f0ca91d365694164db6 Mon Sep 17 00:00:00 2001 From: Peter Larsson Date: Thu, 5 May 2022 07:14:53 +0200 Subject: [PATCH] Add log level support (#58) --- cmd/temporalite/main.go | 37 +++++++++++++++++++++++++++++++++-- internal/liteconfig/config.go | 2 +- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/cmd/temporalite/main.go b/cmd/temporalite/main.go index 87bd0036..b61a74bd 100644 --- a/cmd/temporalite/main.go +++ b/cmd/temporalite/main.go @@ -41,6 +41,7 @@ const ( headlessFlag = "headless" ipFlag = "ip" logFormatFlag = "log-format" + logLevelFlag = "log-level" namespaceFlag = "namespace" pragmaFlag = "sqlite-pragma" ) @@ -111,6 +112,12 @@ func buildCLI() *cli.App { EnvVars: nil, Value: "json", }, + &cli.StringFlag{ + Name: logLevelFlag, + Usage: `customize the log level (allowed: ["debug" "info" "warn" "error" "fatal"])`, + EnvVars: nil, + Value: "info", + }, &cli.StringSliceFlag{ Name: pragmaFlag, Aliases: []string{"sp"}, @@ -133,6 +140,12 @@ func buildCLI() *cli.App { return cli.Exit(fmt.Sprintf("bad value %q passed for flag %q", c.String(logFormatFlag), logFormatFlag), 1) } + switch c.String(logLevelFlag) { + case "debug", "info", "warn", "error", "fatal": + default: + return cli.Exit(fmt.Sprintf("bad value %q passed for flag %q", c.String(logLevelFlag), logLevelFlag), 1) + } + // Check that ip address is valid if c.IsSet(ipFlag) && net.ParseIP(c.String(ipFlag)) == nil { return cli.Exit(fmt.Sprintf("bad value %q passed for flag %q", c.String(ipFlag), ipFlag), 1) @@ -176,8 +189,22 @@ func buildCLI() *cli.App { if c.Bool(ephemeralFlag) { opts = append(opts, temporalite.WithPersistenceDisabled()) } + + var logger log.Logger if c.String(logFormatFlag) == "pretty" { lcfg := zap.NewDevelopmentConfig() + switch c.String(logLevelFlag) { + case "debug": + lcfg.Level.SetLevel(zap.DebugLevel) + case "info": + lcfg.Level.SetLevel(zap.InfoLevel) + case "warn": + lcfg.Level.SetLevel(zap.WarnLevel) + case "error": + lcfg.Level.SetLevel(zap.ErrorLevel) + case "fatal": + lcfg.Level.SetLevel(zap.FatalLevel) + } lcfg.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder l, err := lcfg.Build( zap.WithCaller(false), @@ -186,9 +213,15 @@ func buildCLI() *cli.App { if err != nil { return err } - logger := log.NewZapLogger(l) - opts = append(opts, temporalite.WithLogger(logger)) + logger = log.NewZapLogger(l) + } else { + logger = log.NewZapLogger(log.BuildZapLogger(log.Config{ + Stdout: true, + Level: c.String(logLevelFlag), + OutputFile: "", + })) } + opts = append(opts, temporalite.WithLogger(logger)) s, err := temporalite.NewServer(opts...) if err != nil { diff --git a/internal/liteconfig/config.go b/internal/liteconfig/config.go index 0f83c911..4b624577 100644 --- a/internal/liteconfig/config.go +++ b/internal/liteconfig/config.go @@ -88,7 +88,7 @@ func NewDefaultConfig() (*Config, error) { SQLitePragmas: nil, Logger: log.NewZapLogger(log.BuildZapLogger(log.Config{ Stdout: true, - Level: "debug", + Level: "info", OutputFile: "", })), portProvider: &portProvider{},