From 5a28e9f9e733ffaf401424a2aa725cd230f007c3 Mon Sep 17 00:00:00 2001 From: James Hillyerd Date: Sat, 31 Mar 2018 15:30:36 -0700 Subject: [PATCH] config: Use log level name DEBUG instead of TRACE Add log level parsing into openLog() for #90 --- cmd/inbucket/main.go | 30 +++++++++++++++++++++--------- doc/config.md | 4 ++-- pkg/config/config.go | 2 +- 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/cmd/inbucket/main.go b/cmd/inbucket/main.go index 5622d8f..87c03cd 100644 --- a/cmd/inbucket/main.go +++ b/cmd/inbucket/main.go @@ -11,6 +11,7 @@ import ( "os" "os/signal" "runtime" + "strings" "syscall" "time" @@ -73,13 +74,6 @@ func main() { config.Usage() return } - // Logger setup. - closeLog, err := openLog(*logfile, *logjson) - if err != nil { - fmt.Fprintf(os.Stderr, "Log error: %v\n", err) - os.Exit(1) - } - startupLog := log.With().Str("phase", "startup").Logger() // Process configuration. config.Version = version config.BuildDate = date @@ -92,6 +86,13 @@ func main() { conf.POP3.Debug = true conf.SMTP.Debug = true } + // Logger setup. + closeLog, err := openLog(conf.LogLevel, *logfile, *logjson) + if err != nil { + fmt.Fprintf(os.Stderr, "Log error: %v\n", err) + os.Exit(1) + } + startupLog := log.With().Str("phase", "startup").Logger() // Setup signal handler. sigChan := make(chan os.Signal, 1) signal.Notify(sigChan, syscall.SIGTERM, syscall.SIGINT) @@ -166,9 +167,20 @@ signalLoop: } // openLog configures zerolog output, returns func to close logfile. -func openLog(logfile string, json bool) (close func(), err error) { +func openLog(level string, logfile string, json bool) (close func(), err error) { + switch strings.ToUpper(level) { + case "DEBUG": + zerolog.SetGlobalLevel(zerolog.DebugLevel) + case "INFO": + zerolog.SetGlobalLevel(zerolog.InfoLevel) + case "WARN": + zerolog.SetGlobalLevel(zerolog.WarnLevel) + case "ERROR": + zerolog.SetGlobalLevel(zerolog.ErrorLevel) + default: + return nil, fmt.Errorf("Log level %q not one of: DEBUG, INFO, WARN, ERROR", level) + } close = func() {} - zerolog.SetGlobalLevel(zerolog.DebugLevel) var w io.Writer color := true switch logfile { diff --git a/doc/config.md b/doc/config.md index 5f9fa92..8f8c5ba 100644 --- a/doc/config.md +++ b/doc/config.md @@ -8,7 +8,7 @@ Running `inbucket -help` will yield a condensed summary of the environment variables it supports: KEY DEFAULT DESCRIPTION - INBUCKET_LOGLEVEL INFO TRACE, INFO, WARN, or ERROR + INBUCKET_LOGLEVEL INFO DEBUG, INFO, WARN, or ERROR INBUCKET_SMTP_ADDR 0.0.0.0:2500 SMTP server IP4 host:port INBUCKET_SMTP_DOMAIN inbucket HELO domain INBUCKET_SMTP_DOMAINNOSTORE Load testing domain @@ -47,7 +47,7 @@ should probably select INFO, but a busy shared installation would be better off with WARN or ERROR. - Default: `INFO` -- Values: one of `TRACE`, `INFO`, `WARN`, or `ERROR` +- Values: one of `DEBUG`, `INFO`, `WARN`, or `ERROR` ## SMTP diff --git a/pkg/config/config.go b/pkg/config/config.go index 7db13a8..a8452f1 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -29,7 +29,7 @@ var ( // Root wraps all other configurations. type Root struct { - LogLevel string `required:"true" default:"INFO" desc:"TRACE, INFO, WARN, or ERROR"` + LogLevel string `required:"true" default:"INFO" desc:"DEBUG, INFO, WARN, or ERROR"` SMTP SMTP POP3 POP3 Web Web