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;WithGroupandslog.Groupadd agroup.keyprefix. Empty attributes are dropped andLogValuervalues 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.
Enabledfollows the level of the wrapped logger (including runtime changes).- The
ctxpassed toInfoContextand the like goes throughWithContext, so context extractors work. - The
callerfield points at theslog.Infocall site (thePCfromslog.Recordis used). As for ordinary loggers, it is written only if the most verbose level configured atInitis debug or trace, or forWithError. For loggers not made by this package, the caller they detect themselves is kept. - Redaction and static
fieldsapply, because the attributes pass through the wrapped logger'sWithKeys.