Documentation
¶
Overview ¶
Package logassert captures slog records during a test and offers assertion helpers so tests can verify "this code path emitted hook_overflow with dropped>0" without grepping stderr.
Use Capture as the slog.Handler for a fresh logger; the production code under test should accept a *slog.Logger via dependency injection (or, where global, swap slog.SetDefault for the duration of the test).
cap := logassert.NewCapture()
logger := slog.New(cap)
doWork(logger)
cap.AssertContains(t, "watcher_overflow")
rec := cap.MustOne(t, "hook_status_updated")
require.Equal(t, "running", rec.String("status"))
Records preserve attribute order via flattening — groups become dotted prefixes ("watcher.dropped") so tests can address a single attribute regardless of WithGroup nesting.
Index ¶
- type Capture
- func (c *Capture) AssertContains(t TB, msg string)
- func (c *Capture) AssertNotContains(t TB, msg string)
- func (c *Capture) Enabled(_ context.Context, _ slog.Level) bool
- func (c *Capture) Handle(ctx context.Context, r slog.Record) error
- func (c *Capture) MustOne(t TB, msg string) Record
- func (c *Capture) Records() []Record
- func (c *Capture) Reset()
- func (c *Capture) WithAttrs(attrs []slog.Attr) slog.Handler
- func (c *Capture) WithGroup(name string) slog.Handler
- func (c *Capture) WithMessage(msg string) []Record
- type Record
- type TB
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Capture ¶
type Capture struct {
// contains filtered or unexported fields
}
Capture is the user-facing handle. It owns the storage; child views returned by WithAttrs/WithGroup all forward records back to it.
func (*Capture) AssertContains ¶
AssertContains fails the test if no record has Message == msg.
func (*Capture) AssertNotContains ¶
AssertNotContains fails the test if any record has Message == msg.
func (*Capture) MustOne ¶
MustOne returns the single record with Message == msg, failing the test if zero or multiple match.
func (*Capture) WithGroup ¶
WithGroup returns a child handler that nests subsequent attrs under name.
func (*Capture) WithMessage ¶
WithMessage returns the subset of records whose Message equals msg.
type Record ¶
Record is a captured slog entry with attributes flattened to a dotted-key map (groups become "g.k").