From 9baf82c8c5590ed94b65656e36b29d3bffeed1eb Mon Sep 17 00:00:00 2001 From: jonny rhea Date: Thu, 15 Jan 2026 11:24:25 -0600 Subject: [PATCH] log: cache default level when no vmodule patterns configured and add downgrade test --- log/handler_glog.go | 15 +++++++++++++-- log/logger_test.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/log/handler_glog.go b/log/handler_glog.go index de0456b5a3..1d85b9dfe5 100644 --- a/log/handler_glog.go +++ b/log/handler_glog.go @@ -213,9 +213,20 @@ func (h *GlogHandler) Handle(ctx context.Context, r slog.Record) error { lvl = slog.Level(h.level.Load()) } - // Store only if patterns are still the same slice (avoid caching stale results). + // Check if we should cache this result h.lock.Lock() - if len(h.patterns) > 0 && len(curPatterns) > 0 && &h.patterns[0] == &curPatterns[0] { + shouldCache := false + switch { + case len(curPatterns) == 0 && len(h.patterns) == 0: + // Cache the default/global level to avoid re-evaluating the callsite each time. + shouldCache = true + case len(h.patterns) > 0 && len(curPatterns) > 0: + // Only cache the result if the vmodule patterns have not changed since we + // snapshotted them. This avoids inserting stale entries if Vmodule() updates + // the pattern list concurrently with Handle(). + shouldCache = (&h.patterns[0] == &curPatterns[0]) + } + if shouldCache { cache.Store(r.PC, lvl) } h.lock.Unlock() diff --git a/log/logger_test.go b/log/logger_test.go index dae8497204..c951d7f9f0 100644 --- a/log/logger_test.go +++ b/log/logger_test.go @@ -33,6 +33,35 @@ func TestLoggingWithVmodule(t *testing.T) { } } +func TestLoggingWithVmoduleDowngrade(t *testing.T) { + out := new(bytes.Buffer) + glog := NewGlogHandler(NewTerminalHandlerWithLevel(out, LevelTrace, false)) + glog.Verbosity(LevelTrace) // Allow all logs globally + logger := NewLogger(glog) + + // This should appear (global level allows it) + logger.Info("before vmodule downgrade, this should be logged") + if !bytes.Contains(out.Bytes(), []byte("before vmodule downgrade")) { + t.Fatal("expected 'before vmodule downgrade' to be logged") + } + out.Reset() + + // Downgrade this file to only allow Warn and above + glog.Vmodule("logger_test.go=2") + + // Info should now be filtered out + logger.Info("after vmodule downgrade, this should be filtered") + if bytes.Contains(out.Bytes(), []byte("after vmodule downgrade, this should be filtered")) { + t.Fatal("expected 'after vmodule downgrade, this should be filtered' to NOT be logged after vmodule downgrade") + } + + // Warn should still appear + logger.Warn("after vmodule downgrade, this should be logged") + if !bytes.Contains(out.Bytes(), []byte("after vmodule downgrade, this should be logged")) { + t.Fatal("expected 'should appear' to be logged") + } +} + func TestTerminalHandlerWithAttrs(t *testing.T) { out := new(bytes.Buffer) glog := NewGlogHandler(NewTerminalHandlerWithLevel(out, LevelTrace, false).WithAttrs([]slog.Attr{slog.String("baz", "bat")}))