add env var LOG_COLORS=[1|true|0|false] to toggle colored log output (enabled by default) (#951)

pull/952/head
Thorsten Klein 3 years ago committed by GitHub
parent 793fbce86c
commit 9d00a6468f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 22
      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
}

Loading…
Cancel
Save