Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Feb 1, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"os"
"regexp"
"sort"
"strconv"
"strings"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -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)
Expand All @@ -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
}
Expand Down