Skip to content
This repository has been archived by the owner on Jan 16, 2021. It is now read-only.

Commit

Permalink
feat(gddo-server): make log level configurable, default to info
Browse files Browse the repository at this point in the history
  • Loading branch information
dominicbarnes committed Nov 6, 2019
1 parent 72a348e commit 1ed333a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
8 changes: 8 additions & 0 deletions gddo-server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const (
githubTokenEnvVar = "GITHUB_TOKEN"
githubClientIDEnvVar = "GITHUB_CLIENT_ID"
githubClientSecretEnvVar = "GITHUB_CLIENT_SECRET"
logLevelEnvVar = "LOG_LEVEL"
)

const (
Expand Down Expand Up @@ -68,6 +69,9 @@ const (

// Pub/Sub Config
ConfigCrawlPubSubTopic = "crawl-events"

// Log Config
ConfigLogLevel = "log-level"
)

func loadConfig(ctx context.Context, args []string) (*viper.Viper, error) {
Expand Down Expand Up @@ -111,6 +115,7 @@ func loadConfig(ctx context.Context, args []string) (*viper.Viper, error) {
v.BindEnv(ConfigGithubToken, githubTokenEnvVar)
v.BindEnv(ConfigGithubClientID, githubClientIDEnvVar)
v.BindEnv(ConfigGithubClientSecret, githubClientSecretEnvVar)
v.BindEnv(ConfigLogLevel, logLevelEnvVar)

// Read from config.
if err := readViperConfig(ctx, v); err != nil {
Expand All @@ -120,6 +125,8 @@ func loadConfig(ctx context.Context, args []string) (*viper.Viper, error) {
// Set defaults based on other configs
setDefaults(v)

log.SetLevel(v.GetString(ConfigLogLevel))

log.Debug(ctx, "config values loaded", "values", v.AllSettings())
return v, nil
}
Expand Down Expand Up @@ -173,6 +180,7 @@ func buildFlags() *pflag.FlagSet {
flags.String(ConfigGAERemoteAPI, "", "Remoteapi endpoint for App Engine Search. Defaults to serviceproxy-dot-${project}.appspot.com.")
flags.Float64(ConfigTraceSamplerFraction, 0.1, "Fraction of the requests sampled by the trace API.")
flags.Float64(ConfigTraceSamplerMaxQPS, 5, "Max number of requests sampled every second by the trace API.")
flags.String(ConfigLogLevel, "info", "Determine which level of logs to print.")

return flags
}
Expand Down
8 changes: 8 additions & 0 deletions log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ func FromContext(ctx context.Context) log15.Logger {
return log15.Root()
}

func SetLevel(level string) {
root := log15.Root()
lvl, err := log15.LvlFromString(level)
if err != nil {
root.SetHandler(log15.LvlFilterHandler(lvl, root.GetHandler()))
}
}

// NewContext creates a new context containing the given logger. It is not
// recommended for use and may be removed in the future.
func NewContext(ctx context.Context, l log15.Logger) context.Context {
Expand Down

0 comments on commit 1ed333a

Please sign in to comment.