Skip to content
This repository has been archived by the owner on Apr 19, 2024. It is now read-only.

Commit

Permalink
Introduce log level and format config (#138)
Browse files Browse the repository at this point in the history
  • Loading branch information
tareqmamari authored Apr 21, 2022
1 parent deaf670 commit 3aae9e3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
23 changes: 23 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,8 @@ func (d *DaemonConfig) ServerTLS() *tls.Config {
func SetupDaemonConfig(logger *logrus.Logger, configFile string) (DaemonConfig, error) {
log := logrus.NewEntry(logger)
var conf DaemonConfig
var logLevel string
var logFormat string
var advAddr, advPort string
var err error

Expand All @@ -257,10 +259,31 @@ func SetupDaemonConfig(logger *logrus.Logger, configFile string) (DaemonConfig,
}
}

// Log config
setter.SetDefault(&logFormat, os.Getenv("GUBER_LOG_FORMAT"))
if logFormat != "" {
switch logFormat {
case "json":
logger.SetFormatter(&logrus.JSONFormatter{})
case "text":
logger.SetFormatter(&logrus.TextFormatter{})
default:
return conf, errors.New("GUBER_LOG_FORMAT is invalid; expected value is either json or text")
}
}

setter.SetDefault(&DebugEnabled, getEnvBool(log, "GUBER_DEBUG"))
setter.SetDefault(&logLevel, os.Getenv("GUBER_LOG_LEVEL"))
if DebugEnabled {
logger.SetLevel(logrus.DebugLevel)
log.Debug("Debug enabled")
} else if logLevel != "" {
logrusLogLevel, err := logrus.ParseLevel(logLevel)
if err != nil {
return conf, errors.Wrap(err, "invalid log level")
}

logger.SetLevel(logrusLogLevel)
}

// Main config
Expand Down
10 changes: 10 additions & 0 deletions example.conf
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ GUBER_ADVERTISE_ADDRESS=localhost:9990
# See https://pkg.go.dev/github.com/prometheus/[email protected]/prometheus/collectors#NewGoCollector
# GUBER_METRIC_FLAGS=os,golang

############################
# Log Config
############################

# Log Level, these are the log levels for logrus.
# GUBER_LOG_LEVEL=trace

# Log Format, currently supports either json or text
# GUBER_LOG_FORMAT=json


############################
# Behavior Config
Expand Down

0 comments on commit 3aae9e3

Please sign in to comment.