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

Commit 6de209a

Browse files
committed
feat(gddo-server): make log level configurable, default to info
1 parent 72a348e commit 6de209a

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

gddo-server/config.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const (
2323
githubTokenEnvVar = "GITHUB_TOKEN"
2424
githubClientIDEnvVar = "GITHUB_CLIENT_ID"
2525
githubClientSecretEnvVar = "GITHUB_CLIENT_SECRET"
26+
logLevelEnvVar = "LOG_LEVEL"
2627
)
2728

2829
const (
@@ -68,6 +69,9 @@ const (
6869

6970
// Pub/Sub Config
7071
ConfigCrawlPubSubTopic = "crawl-events"
72+
73+
// Log Config
74+
ConfigLogLevel = "log-level"
7175
)
7276

7377
func loadConfig(ctx context.Context, args []string) (*viper.Viper, error) {
@@ -111,6 +115,7 @@ func loadConfig(ctx context.Context, args []string) (*viper.Viper, error) {
111115
v.BindEnv(ConfigGithubToken, githubTokenEnvVar)
112116
v.BindEnv(ConfigGithubClientID, githubClientIDEnvVar)
113117
v.BindEnv(ConfigGithubClientSecret, githubClientSecretEnvVar)
118+
v.BindEnv(ConfigLogLevel, logLevelEnvVar)
114119

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

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

177185
return flags
178186
}

log/log.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ func FromContext(ctx context.Context) log15.Logger {
2626
return log15.Root()
2727
}
2828

29+
func SetLevel(level string) {
30+
root := log.Root()
31+
root.SetHandler(log.LvlFilterHandler(log.LvlFromString(level), root.GetHandler()))
32+
}
33+
2934
// NewContext creates a new context containing the given logger. It is not
3035
// recommended for use and may be removed in the future.
3136
func NewContext(ctx context.Context, l log15.Logger) context.Context {

0 commit comments

Comments
 (0)