mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-02-26 15:47:21 +00:00
log: cache default level when no vmodule patterns configured and add downgrade test
This commit is contained in:
parent
1423367b4f
commit
9baf82c8c5
2 changed files with 42 additions and 2 deletions
|
|
@ -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()
|
||||
|
|
|
|||
|
|
@ -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")}))
|
||||
|
|
|
|||
Loading…
Reference in a new issue