log: restructure handle function

This commit is contained in:
Felix Lange 2026-04-16 00:11:45 +02:00
parent 37587a7cfc
commit 7a27285f37

View file

@ -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)