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

Change log level #300

Merged
merged 2 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
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
32 changes: 31 additions & 1 deletion gateway/common/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
log "log/slog"
"os"
"strconv"
"strings"
"sync"
)

Expand All @@ -21,12 +22,14 @@ const (
REDIS_USER = "REDIS_USER"
REDIS_PASSWORD = "REDIS_PASSWORD"
INSTRUMENT_ENABLED = "ENABLE_INSTRUMENT"
LOG_LEVEL = "LOG_LEVEL"
)

// This may evolve to include config outside env, or use .env file for
// convenience
type Config struct {
defaults map[string]string
loglevel *log.LevelVar
}

var config *Config
Expand All @@ -36,13 +39,17 @@ func setupConfig() *Config {
configMutex.Do(func() {
config = &Config{
defaults: make(map[string]string),
loglevel: &log.LevelVar{},
}
config.defaults[SHUTDOWN_DELAY] = "5"
config.defaults[JOB_BUFFER_SIZE] = "50"
config.defaults[NUM_WORKERS] = "10"
config.defaults[HOST] = "localhost"
config.defaults[PORT] = "9000"
config.defaults[INSTRUMENT_ENABLED] = "false"

level := parseLogLevel(os.Getenv(LOG_LEVEL))
config.SetLogLevel(level)
})
return config
}
Expand All @@ -57,7 +64,7 @@ func GetConfig(key string) string {
if ok {
return defaultval
} else {
log.Warn("config not set no default", "key", key)
log.Warn("config not set, no default", "key", key)
return ""
}
}
Expand All @@ -81,3 +88,26 @@ func Enabled(key string) bool {
}
return boolval
}

func GetLogLevel() log.Level {
configval := GetConfig(LOG_LEVEL)
return parseLogLevel(configval)
}

func parseLogLevel(level string) log.Level {
if strings.EqualFold(level, "ERROR") {
return log.LevelError
} else if strings.EqualFold(level, "WARN") {
return log.LevelWarn
} else if strings.EqualFold(level, "DEBUG") {
return log.LevelDebug
} else {
return log.LevelInfo
}
}

func (c *Config) SetLogLevel(level log.Level) {
c.loglevel.Set(level)
logger := log.New(log.NewTextHandler(os.Stdout, &log.HandlerOptions{Level: c.loglevel}))
log.SetDefault(logger)
}
3 changes: 2 additions & 1 deletion gateway/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ import (
func gateway() {
// Start job queue
common.GetTaskQueue().SetupWorkers()
level := common.GetLogLevel()

log.Info("starting gateway")
log.Info("starting gateway", "level", level)
proxy.Start()

done := make(chan os.Signal, 1)
Expand Down