From 9d00a6468fa585331c9d7854cfbd9a5180d0abf3 Mon Sep 17 00:00:00 2001 From: Thorsten Klein Date: Tue, 1 Feb 2022 13:59:06 +0100 Subject: [PATCH] add env var LOG_COLORS=[1|true|0|false] to toggle colored log output (enabled by default) (#951) --- cmd/root.go | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index bd387230..65021688 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -30,6 +30,7 @@ import ( "os" "regexp" "sort" + "strconv" "strings" "github.com/spf13/cobra" @@ -147,22 +148,17 @@ func Execute() { // initLogging initializes the logger func initLogging() { + l.Log().SetLevel(logrus.InfoLevel) // default log level: info if flags.traceLogging { l.Log().SetLevel(logrus.TraceLevel) } else if flags.debugLogging { l.Log().SetLevel(logrus.DebugLevel) } else { - switch logLevel := strings.ToUpper(os.Getenv("LOG_LEVEL")); logLevel { - case "TRACE": - l.Log().SetLevel(logrus.TraceLevel) - case "DEBUG": - l.Log().SetLevel(logrus.DebugLevel) - case "WARN": - l.Log().SetLevel(logrus.WarnLevel) - case "ERROR": - l.Log().SetLevel(logrus.ErrorLevel) - default: - l.Log().SetLevel(logrus.InfoLevel) + if ll := os.Getenv("LOG_LEVEL"); ll != "" { + level, err := logrus.ParseLevel(ll) + if err != nil { + l.Log().SetLevel(level) + } } } l.Log().SetOutput(io.Discard) @@ -188,6 +184,10 @@ func initLogging() { ForceColors: true, } + if logColor, err := strconv.ParseBool(os.Getenv("LOG_COLORS")); err == nil { + formatter.ForceColors = logColor + } + if flags.timestampedLogging || os.Getenv("LOG_TIMESTAMPS") != "" { formatter.FullTimestamp = true }