Skip to content

Commit

Permalink
Merge pull request #19170 from callthingsoff/simplify_MergeOutputPaths
Browse files Browse the repository at this point in the history
client/pkg/logutil: simplify MergeOutputPaths function
  • Loading branch information
ahrtr authored Jan 14, 2025
2 parents 5813dce + 2386cd4 commit b3c065a
Showing 1 changed file with 16 additions and 31 deletions.
47 changes: 16 additions & 31 deletions client/pkg/logutil/zap.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
package logutil

import (
"sort"
"slices"
"time"

"go.uber.org/zap"
Expand Down Expand Up @@ -72,37 +72,22 @@ var DefaultZapLoggerConfig = zap.Config{

// MergeOutputPaths merges logging output paths, resolving conflicts.
func MergeOutputPaths(cfg zap.Config) zap.Config {
outputs := make(map[string]struct{})
for _, v := range cfg.OutputPaths {
outputs[v] = struct{}{}
}
outputSlice := make([]string, 0)
if _, ok := outputs["/dev/null"]; ok {
// "/dev/null" to discard all
outputSlice = []string{"/dev/null"}
} else {
for k := range outputs {
outputSlice = append(outputSlice, k)
}
}
cfg.OutputPaths = outputSlice
sort.Strings(cfg.OutputPaths)
cfg.OutputPaths = mergePaths(cfg.OutputPaths)
cfg.ErrorOutputPaths = mergePaths(cfg.ErrorOutputPaths)
return cfg
}

errOutputs := make(map[string]struct{})
for _, v := range cfg.ErrorOutputPaths {
errOutputs[v] = struct{}{}
func mergePaths(old []string) []string {
if len(old) == 0 {
// the original implementation ensures the result is non-nil
return []string{}
}
errOutputSlice := make([]string, 0)
if _, ok := errOutputs["/dev/null"]; ok {
// "/dev/null" to discard all
errOutputSlice = []string{"/dev/null"}
} else {
for k := range errOutputs {
errOutputSlice = append(errOutputSlice, k)
}
// use "/dev/null" to discard all
if slices.Contains(old, "/dev/null") {
return []string{"/dev/null"}
}
cfg.ErrorOutputPaths = errOutputSlice
sort.Strings(cfg.ErrorOutputPaths)

return cfg
// clone a new one; don't modify the original, in case it matters.
dup := slices.Clone(old)
slices.Sort(dup)
return slices.Compact(dup)
}

0 comments on commit b3c065a

Please sign in to comment.