mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-06-19 13:21:37 +00:00
log: restructure handle function
This commit is contained in:
parent
37587a7cfc
commit
7a27285f37
1 changed files with 17 additions and 19 deletions
|
|
@ -167,28 +167,26 @@ func (h *GlogHandler) Handle(ctx context.Context, r slog.Record) error {
|
||||||
func (h *GlogHandler) handle(ctx context.Context, r slog.Record, origin slog.Handler) error {
|
func (h *GlogHandler) handle(ctx context.Context, r slog.Record, origin slog.Handler) error {
|
||||||
cfg := h.config.Load()
|
cfg := h.config.Load()
|
||||||
|
|
||||||
// Fast path: cache hit
|
var lvl slog.Level
|
||||||
if lvl, ok := cfg.cache.Load(r.PC); ok {
|
cachedLvl, ok := cfg.cache.Load(r.PC)
|
||||||
if lvl.(slog.Level) <= r.Level {
|
if ok {
|
||||||
return origin.Handle(ctx, r)
|
// Fast path: cache hit
|
||||||
|
lvl = cachedLvl.(slog.Level)
|
||||||
|
} else {
|
||||||
|
// Resolve the callsite file.
|
||||||
|
fs := runtime.CallersFrames([]uintptr{r.PC})
|
||||||
|
frame, _ := fs.Next()
|
||||||
|
file := frame.File
|
||||||
|
// Match against patterns and cache the level applied at this callsite.
|
||||||
|
lvl = cfg.level // default: use global level
|
||||||
|
for _, rule := range cfg.patterns {
|
||||||
|
if rule.pattern.MatchString("+" + file) {
|
||||||
|
lvl = rule.level
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return nil
|
cfg.cache.Store(r.PC, lvl)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Resolve the callsite file once.
|
|
||||||
fs := runtime.CallersFrames([]uintptr{r.PC})
|
|
||||||
frame, _ := fs.Next()
|
|
||||||
file := frame.File
|
|
||||||
|
|
||||||
// Match against patterns and cache the level applied at this callsitte
|
|
||||||
lvl := cfg.level // default: use global level
|
|
||||||
for _, rule := range cfg.patterns {
|
|
||||||
if rule.pattern.MatchString("+" + file) {
|
|
||||||
lvl = rule.level
|
|
||||||
}
|
|
||||||
}
|
|
||||||
cfg.cache.Store(r.PC, lvl)
|
|
||||||
|
|
||||||
// Handle the message.
|
// Handle the message.
|
||||||
if lvl <= r.Level {
|
if lvl <= r.Level {
|
||||||
return origin.Handle(ctx, r)
|
return origin.Handle(ctx, r)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue