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,27 +167,25 @@ 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()
var lvl slog.Level
cachedLvl, ok := cfg.cache.Load(r.PC)
if ok {
// Fast path: cache hit // Fast path: cache hit
if lvl, ok := cfg.cache.Load(r.PC); ok { lvl = cachedLvl.(slog.Level)
if lvl.(slog.Level) <= r.Level { } else {
return origin.Handle(ctx, r) // Resolve the callsite file.
}
return nil
}
// Resolve the callsite file once.
fs := runtime.CallersFrames([]uintptr{r.PC}) fs := runtime.CallersFrames([]uintptr{r.PC})
frame, _ := fs.Next() frame, _ := fs.Next()
file := frame.File file := frame.File
// Match against patterns and cache the level applied at this callsite.
// Match against patterns and cache the level applied at this callsitte lvl = cfg.level // default: use global level
lvl := cfg.level // default: use global level
for _, rule := range cfg.patterns { for _, rule := range cfg.patterns {
if rule.pattern.MatchString("+" + file) { if rule.pattern.MatchString("+" + file) {
lvl = rule.level lvl = rule.level
} }
} }
cfg.cache.Store(r.PC, lvl) cfg.cache.Store(r.PC, lvl)
}
// Handle the message. // Handle the message.
if lvl <= r.Level { if lvl <= r.Level {