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 ¶
- func SwapDefault(tb testing.TB, logger *slog.Logger)
- type Recorder
- func (rec *Recorder) CountMsg(sub string) int
- func (*Recorder) Enabled(context.Context, slog.Level) bool
- func (rec *Recorder) Handle(_ context.Context, r slog.Record) error
- func (rec *Recorder) Records(sub string) []slog.Record
- func (rec *Recorder) WithAttrs(attrs []slog.Attr) slog.Handler
- func (rec *Recorder) WithGroup(group string) slog.Handler
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func SwapDefault ¶
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 ¶
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 ¶
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 ¶
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 ¶
Enabled reports true for every level so nothing is filtered before capture.
func (*Recorder) Handle ¶
Handle records r with the handler's attributes and groups applied. It satisfies slog.Handler.
func (*Recorder) Records ¶
Records returns clones of the captured records whose message contains sub, in the order they were logged.