Skip to content

Latest commit

 

History

History
16 lines (13 loc) · 1.39 KB

slog.md

File metadata and controls

16 lines (13 loc) · 1.39 KB

slog integration (Go 1.21+)

logging.MustInit()
slog.SetDefault(logging.NewSlogLogger(logging.NewLogger()))
slog.Info("hello", "user", 42, slog.Group("req", "id", "r-1"))

NewSlogHandler(logger) returns a slog.Handler and NewSlogLogger(logger) returns a ready *slog.Logger. The code lives in slog_go121.go, built under go1.21 && !logging_legacy; on older Go versions or with the logging_legacy tag these functions do not exist.

  • Attributes become Keys; WithGroup and slog.Group add a group.key prefix. Empty attributes are dropped and LogValuer values are resolved. Duplicate keys overwrite each other within one record.
  • Levels: below Debug is Trace, then Debug, Info, Warn, Error (Error and everything above it is written as Error). The adapter never produces Panic or Fatal records.
  • Enabled follows the level of the wrapped logger (including runtime changes).
  • The ctx passed to InfoContext and the like goes through WithContext, so context extractors work.
  • The caller field points at the slog.Info call site (the PC from slog.Record is used). As for ordinary loggers, it is written only if the most verbose level configured at Init is debug or trace, or for WithError. For loggers not made by this package, the caller they detect themselves is kept.
  • Redaction and static fields apply, because the attributes pass through the wrapped logger's WithKeys.