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...). Not logger.Info, logger.Warn, or any of the kv-flavored helpers.
  • Every attribute comes from a helper in internal/log/attrs.go. Write applog.OrderID(o.ID), never slog.String("order_id", o.ID) inline.
  • sloglint enforces all three on every commit so the workflow doesn’t erode.