Skip to content

Commit

Permalink
fix: envvar syntax and labels struct type
Browse files Browse the repository at this point in the history
  • Loading branch information
lanzafame committed Feb 25, 2021
1 parent 509684e commit 29f70aa
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
18 changes: 8 additions & 10 deletions setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const (
envLoggingURL = "GOLOG_URL" // url that will be processed by sink in the zap

envLoggingOutput = "GOLOG_OUTPUT" // possible values: stdout|stderr|file combine multiple values with '+'
envLoggingLabels = "GOLOG_LOG_LABELS" // comma-separated key-value pairs, i.e. "app,example_app,dc,sjc-1"
envLoggingLabels = "GOLOG_LOG_LABELS" // comma-separated key-value pairs, i.e. "app=example_app,dc=sjc-1"
)

type LogFormat int
Expand Down Expand Up @@ -64,7 +64,7 @@ type Config struct {
URL string

// Labels is a set of key-values to apply to all loggers
Labels []string
Labels map[string]string
}

// ErrNoSuchLogger is returned when the util pkg is asked for a non existant logger
Expand Down Expand Up @@ -127,10 +127,8 @@ func SetupLogging(cfg Config) {

newPrimaryCore := newCore(primaryFormat, ws, LevelDebug) // the main core needs to log everything.

if len(cfg.Labels) > 0 {
for i := 0; i < len(cfg.Labels); i=i+2 {
newPrimaryCore = newPrimaryCore.With([]zap.Field{zap.String(cfg.Labels[i], cfg.Labels[i+1])})
}
for k, v := range cfg.Labels {
newPrimaryCore = newPrimaryCore.With([]zap.Field{zap.String(k, v)})
}

if primaryCore != nil {
Expand Down Expand Up @@ -251,6 +249,7 @@ func configFromEnv() Config {
Format: ColorizedOutput,
Stderr: true,
Level: LevelError,
Labels: map[string]string{},
}

format := os.Getenv(envLoggingFmt)
Expand Down Expand Up @@ -307,10 +306,9 @@ func configFromEnv() Config {
labels := os.Getenv(envLoggingLabels)
if labels != "" {
labelKVs := strings.Split(labels, ",")
if len(labelKVs)%2 != 0 {
fmt.Fprint(os.Stderr, "odd number of args for GOLOG_LOG_LABELS; please specify complete key-value pairs")
} else {
cfg.Labels = labelKVs
for _, label := range labelKVs {
kv := strings.Split(label, "=")
cfg.Labels[kv[0]] = kv[1]
}
}

Expand Down
2 changes: 1 addition & 1 deletion setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func TestLogLabels(t *testing.T) {
}()

// set the go-log labels env var
os.Setenv(envLoggingLabels, "app,example_app,dc,sjc-1")
os.Setenv(envLoggingLabels, "app=example_app,dc=sjc-1")
SetupLogging(configFromEnv())

log := getLogger("test")
Expand Down

0 comments on commit 29f70aa

Please sign in to comment.