Documentation
¶
Overview ¶
Package sloghandler exposes a log/slog.Handler backed by a [*loglayer.LogLayer], so any code emitting through slog (your own `slog.Info(...)` calls, or library code that accepts a `*slog.Logger`) flows through loglayer's plugin pipeline, fan-out, and groups.
Mental model: slog has two halves. `*slog.Logger` is the frontend API (`slog.Info("msg", "k", "v")`); `slog.Handler` is the sink that actually emits the record. This package implements the sink and routes every record into a loglayer logger, so the rest of loglayer's setup (redact plugin, oteltrace, multi-transport fan-out, level filters) applies.
Direction matters: `transports/slog` is the opposite. It lets loglayer emit through a slog logger as a backend. Use this package when slog is the frontend you're calling and loglayer is what you want behind it.
Usage:
log := loglayer.New(loglayer.Config{Transport: structured.New(structured.Config{})})
log.AddPlugin(redact.New(redact.Config{Keys: []string{"password"}}))
slog.SetDefault(slog.New(sloghandler.New(log)))
slog.Info("user signed in", "userId", 42, "password", "hunter2")
// → flows through loglayer; redact plugin replaces the password.
Mapping summary:
- slog levels Debug/Info/Warn/Error map onto the matching loglayer levels. slog has no Fatal; values at or above slog.LevelError map to LogLevelError so a raw slog level can never trigger loglayer's Fatal exit. (You can still call log.Fatal directly.)
- Attrs added via `slog.With(...)` / `Handler.WithAttrs` accumulate on the handler and become persistent fields on every emission handled by it.
- Attrs added inline on a record (`slog.Info("msg", "k", "v")`) become per-call fields for that emission.
- `Handler.WithGroup(name)` opens a nested map under name; subsequent attrs land under it. Empty groups (no attrs added under them) are dropped on emission, matching slog's documented behavior.
- The context passed to Handle is forwarded via Raw so dispatch-time plugins (oteltrace, datadogtrace) see the request's context.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
Handler is a slog.Handler that emits records through a loglayer logger. Construct with New. Safe for concurrent use; WithAttrs and WithGroup return new Handler values without mutating the receiver.
func New ¶
func New(log *loglayer.LogLayer) *Handler
New constructs an slog.Handler that emits through log.
The returned handler's WithAttrs / WithGroup return derived handlers that share the same underlying loglayer logger.
func (*Handler) Enabled ¶
Enabled reports whether log calls at level should be emitted, by translating to the matching loglayer level and consulting the underlying logger's level state.
func (*Handler) Handle ¶
Handle converts the record into a loglayer Raw entry and dispatches. Per slog.Handler contract, it should not retain r.