Documentation
¶
Overview ¶
Package capture is an auth-internal test helper: a slog.Handler that records log records so tests can assert on auth's logging decisions, plus a helper to install it as the default logger.
It is deliberately auth-internal rather than a dependency on an external logging helper (e.g. slogx/capture): auth is a foundational library, and a ~50-line, never-changing test handler is not worth a new module edge. Import it only from auth's _test.go files.
Index ¶
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. 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.