From 6d8fc185aefdd1805c18dfad0fc618dd089b4f07 Mon Sep 17 00:00:00 2001 From: iwilltry42 Date: Tue, 1 Feb 2022 13:57:04 +0100 Subject: [PATCH] add env var LOG_COLORS=[1|true|0|false] to toggle colored log output (enabled by default) --- cmd/root.go | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index bd3872305..650216880 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 }