capture

package
v1.0.0-dev.17 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package capture is an access-internal test helper: a slog.Handler that records log records so tests can assert on access's logging decisions, plus a helper to install it as the default logger.

It is deliberately access-internal rather than a dependency on an external logging helper (e.g. slogx/capture): access is a foundational library, and a small test handler is not worth a new module edge. Import it only from access's _test.go files.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SwapDefault

func SwapDefault(tb testing.TB, logger *slog.Logger)

SwapDefault installs logger as slog's default for the test and restores all three globals slog.SetDefault writes. It also points the log package at the installed handler and SKIPS that redirect for slog's own default handler, so restoring slog alone leaves log writing into a buffer nothing reads. slog goes back first: reinstalling a non-default handler re-runs the redirect and would undo an earlier log restore. Use it directly when the test asserts through its own handler rather than a Recorder; it mutates global state, so the test must not run in parallel.

Types

type Recorder

type Recorder struct {
	// contains filtered or unexported fields
}

Recorder is a slog.Handler that captures every record regardless of level so tests can assert on what was logged. A record keeps the attributes and groups of the WithAttrs and WithGroup calls that made its handler, and every handler derived from one Recorder records into the same log. It is safe for concurrent use.

func Default

func Default(tb testing.TB) *Recorder

Default installs a fresh Recorder as slog's default logger and restores the previous default via tb.Cleanup. Use it for code that logs through slog.Default(); because it mutates global state the test must not run in parallel.

func New

func New() (*slog.Logger, *Recorder)

New returns a Recorder and an *slog.Logger writing to it, for code that takes an injected logger. It does not touch the global default, so a test using it may run in parallel.

func (*Recorder) CountMsg

func (rec *Recorder) CountMsg(sub string) int

CountMsg returns how many captured records have a message containing sub. Reached only by tests, like the rest of this package — see the package doc.

func (*Recorder) Enabled

func (*Recorder) Enabled(context.Context, slog.Level) bool

Enabled reports true for every level so nothing is filtered before capture.

func (*Recorder) Handle

func (rec *Recorder) Handle(_ context.Context, r slog.Record) error

Handle records r with the handler's attributes and groups applied. It satisfies slog.Handler.

func (*Recorder) Records

func (rec *Recorder) Records(sub string) []slog.Record

Records returns clones of the captured records whose message contains sub, in the order they were logged.

func (*Recorder) WithAttrs

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

WithAttrs returns a handler recording attrs on every record, inside the groups opened before it.

func (*Recorder) WithGroup

func (rec *Recorder) WithGroup(group string) slog.Handler

WithGroup returns a handler recording every later attribute inside group; an empty name returns rec, as slog.Handler requires.

Jump to

Keyboard shortcuts

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