Skip to content
This repository has been archived by the owner on Apr 3, 2024. It is now read-only.

Commit

Permalink
Add log level support (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
plarsson authored May 5, 2022
1 parent 037b599 commit 715fc8d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
37 changes: 35 additions & 2 deletions cmd/temporalite/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const (
headlessFlag = "headless"
ipFlag = "ip"
logFormatFlag = "log-format"
logLevelFlag = "log-level"
namespaceFlag = "namespace"
pragmaFlag = "sqlite-pragma"
)
Expand Down Expand Up @@ -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"},
Expand All @@ -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)
Expand Down Expand Up @@ -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),
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion internal/liteconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{},
Expand Down

0 comments on commit 715fc8d

Please sign in to comment.