mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-06-12 01:41:36 +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 {
|
||||
cfg := h.config.Load()
|
||||
|
||||
// Fast path: cache hit
|
||||
if lvl, ok := cfg.cache.Load(r.PC); ok {
|
||||
if lvl.(slog.Level) <= r.Level {
|
||||
return origin.Handle(ctx, r)
|
||||
var lvl slog.Level
|
||||
cachedLvl, ok := cfg.cache.Load(r.PC)
|
||||
if ok {
|
||||
// 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.
|
||||
if lvl <= r.Level {
|
||||
return origin.Handle(ctx, r)
|
||||
|
|
|
|||
Loading…
Reference in a new issue