charmbracelet / log

A minimal, colorful Go logging library 🪵
MIT License
2.4k stars 67 forks source link

slog: Don't log if not enabled at level #103

Closed imjasonh closed 9 months ago

imjasonh commented 9 months ago

This is a very naive first attempt to fix a potential bug I noticed in this package's integration with slog.

When I used

slog.SetDefault(slog.New(log.NewWithOptions(out, log.Options{Level: WarnLevel})))

I was still seeing INFO and DEBU logs, and I couldn't figure out why. I think this is the cause -- the slog Handle method isn't consulting Enabled before handling. slog's builtin Text and JSON handlers consult the enabled level before logging.

Patching this locally fixed the issue for me, so I thought I'd contribute it. Please let me know if this is on the wrong track.

aymanbagabas commented 9 months ago

@imjasonh Good catch! Looks good, thanks!