sloghandler

package
v1.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 29, 2026 License: MIT Imports: 4 Imported by: 0

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

func (h *Handler) Enabled(_ context.Context, level slog.Level) bool

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

func (h *Handler) Handle(ctx context.Context, r slog.Record) error

Handle converts the record into a loglayer Raw entry and dispatches. Per slog.Handler contract, it should not retain r.

func (*Handler) WithAttrs

func (h *Handler) WithAttrs(attrs []slog.Attr) slog.Handler

WithAttrs returns a derived handler whose subsequent records carry attrs as persistent fields, nested under the current group path.

func (*Handler) WithGroup

func (h *Handler) WithGroup(name string) slog.Handler

WithGroup returns a derived handler that nests subsequent attrs under name. Per slog convention, an empty name is a no-op.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL