Type-safe slogging
Well written article by Redowan Delowar, explaining how to achieve type-safe logging with Go's log/slog library.
- Take the logger as a constructor argument. Never reach for
slog.Default()or any package-level slog function.- Always use
logger.LogAttrs(ctx, level, msg, attrs...). Notlogger.Info,logger.Warn, or any of the kv-flavored helpers.- Every attribute comes from a helper in
internal/log/attrs.go. Writeapplog.OrderID(o.ID), neverslog.String("order_id", o.ID)inline.- sloglint enforces all three on every commit so the workflow doesn’t erode.