Documentation
¶
Overview ¶
Package loggertest provides shared testing utilities for the global logger package: a slog handler that records every emitted record and helpers for inspecting them.
This is testing-only — it lives in its own package so production code can't accidentally depend on it, but assertions across services can reuse the same capture implementation instead of redefining one in every test file.
Index ¶
- func FlatAttrs(r slog.Record) map[string]any
- func PCFrame(pc uintptr) runtime.Frame
- type CaptureHandler
- func (h *CaptureHandler) Enabled(_ context.Context, _ slog.Level) bool
- func (h *CaptureHandler) Find(t *testing.T, msg string) slog.Record
- func (h *CaptureHandler) Handle(_ context.Context, r slog.Record) error
- func (h *CaptureHandler) Last(t *testing.T) slog.Record
- func (h *CaptureHandler) Records() []slog.Record
- func (h *CaptureHandler) Since(idx int) []slog.Record
- func (h *CaptureHandler) Snapshot() int
- func (h *CaptureHandler) WithAttrs(_ []slog.Attr) slog.Handler
- func (h *CaptureHandler) WithGroup(_ string) slog.Handler
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type CaptureHandler ¶
type CaptureHandler struct {
// contains filtered or unexported fields
}
CaptureHandler is a slog.Handler that stores every log record it receives. Install it into the global logger via Install (or call logger.AddHandler directly) and inspect the captured records in assertions.
Safe for concurrent use.
func Install ¶
func Install(t *testing.T) *CaptureHandler
Install registers a fresh CaptureHandler with the global logger and returns it. Every subsequent log call (via logger.Error, slog.Info, wide events, etc.) will be recorded.
The global logger keeps a reference to the handler for the rest of the process; tests should not rely on cleanup between runs. Use CaptureHandler.Records and CaptureHandler.Snapshot to scope assertions to records emitted after a known point.
func New ¶
func New() *CaptureHandler
New returns an empty CaptureHandler. Most tests should use Install instead, which both constructs the handler and wires it into the global logger.
func (*CaptureHandler) Find ¶
Find returns the first captured record whose message equals msg. Tests use this instead of indexing when the global logger is shared and the order of records isn't predictable.
func (*CaptureHandler) Last ¶
func (h *CaptureHandler) Last(t *testing.T) slog.Record
Last returns the most recent record, failing the test if none have been captured.
func (*CaptureHandler) Records ¶
func (h *CaptureHandler) Records() []slog.Record
Records returns a copy of every record captured so far.
func (*CaptureHandler) Since ¶
func (h *CaptureHandler) Since(idx int) []slog.Record
Since returns all records captured after the given snapshot index.
func (*CaptureHandler) Snapshot ¶
func (h *CaptureHandler) Snapshot() int
Snapshot returns the current record count. Pair with CaptureHandler.Since to assert only on records emitted by a specific block of test code, which matters because the global logger is shared across tests.