From e25cb5ce80dc827a7145455f4fa1b3261f33f735 Mon Sep 17 00:00:00 2001 From: Jared Wasinger Date: Wed, 15 Nov 2023 16:22:17 +0800 Subject: [PATCH] log: copy siteCache map when instantiating GlogHandler via WithAttrs method --- internal/debug/flags.go | 12 ++++++------ log/handler_glog.go | 18 +++++++++++------- log/logger_test.go | 4 ---- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/internal/debug/flags.go b/internal/debug/flags.go index 81b8bb6902..23e4745e8c 100644 --- a/internal/debug/flags.go +++ b/internal/debug/flags.go @@ -228,10 +228,10 @@ func Setup(ctx *cli.Context) error { output = io.MultiWriter(terminalOutput, logOutputFile) } else if logFile != "" { var err error - if logOutputF, err = os.OpenFile(logFile, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644); err != nil { + if logOutputFile, err = os.OpenFile(logFile, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644); err != nil { return err } - output = io.MultiWriter(logOutputF, terminalOutput) + output = io.MultiWriter(logOutputFile, terminalOutput) context = append(context, "location", logFile) } else { output = terminalOutput @@ -250,8 +250,8 @@ func Setup(ctx *cli.Context) error { useColor := (isatty.IsTerminal(os.Stderr.Fd()) || isatty.IsCygwinTerminal(os.Stderr.Fd())) && os.Getenv("TERM") != "dumb" if useColor { terminalOutput = colorable.NewColorableStderr() - if logOutputF != nil { - output = io.MultiWriter(logOutputF, terminalOutput) + if logOutputFile != nil { + output = io.MultiWriter(logOutputFile, terminalOutput) } else { output = terminalOutput } @@ -337,8 +337,8 @@ func StartPProf(address string, withMetrics bool) { func Exit() { Handler.StopCPUProfile() Handler.StopGoTrace() - if logOutputF != nil { - logOutputF.Close() + if logOutputFile != nil { + logOutputFile.Close() } } diff --git a/log/handler_glog.go b/log/handler_glog.go index 4a73599b97..d26a03c410 100644 --- a/log/handler_glog.go +++ b/log/handler_glog.go @@ -145,14 +145,18 @@ func (h *GlogHandler) Enabled(ctx context.Context, lvl slog.Level) bool { } func (h *GlogHandler) WithAttrs(attrs []slog.Attr) slog.Handler { + h.lock.RLock() + siteCache := make(map[uintptr]slog.Level) + for k, v := range h.siteCache { + siteCache[k] = v + } + h.lock.RUnlock() + res := GlogHandler{ - h.origin.WithAttrs(attrs), - atomic.Int32{}, - atomic.Bool{}, - h.patterns, - h.siteCache, - h.location, - sync.RWMutex{}, + origin: h.origin.WithAttrs(attrs), + patterns: h.patterns, + siteCache: siteCache, + location: h.location, } res.level.Store(h.level.Load()) diff --git a/log/logger_test.go b/log/logger_test.go index 6bdfa16ccd..fca1f1680f 100644 --- a/log/logger_test.go +++ b/log/logger_test.go @@ -51,7 +51,3 @@ func BenchmarkTraceLogging(b *testing.B) { Trace("a message", "v", i) } } - -func TestLoggingWithAttrs(t *testing.T) { - // TODO: test logger.With(...) -}