logassert

package
v1.9.6 Latest Latest
Warning

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

Go to latest
Published: May 15, 2026 License: MIT Imports: 5 Imported by: 0

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

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 NewCapture

func NewCapture() *Capture

NewCapture returns a fresh Capture handler.

func (*Capture) AssertContains

func (c *Capture) AssertContains(t TB, msg string)

AssertContains fails the test if no record has Message == msg.

func (*Capture) AssertNotContains

func (c *Capture) AssertNotContains(t TB, msg string)

AssertNotContains fails the test if any record has Message == msg.

func (*Capture) Enabled

func (c *Capture) Enabled(_ context.Context, _ slog.Level) bool

Enabled reports whether the handler handles records at lvl. Always true.

func (*Capture) Handle

func (c *Capture) Handle(ctx context.Context, r slog.Record) error

Handle stores the record (as if from a root view with no group / attrs).

func (*Capture) MustOne

func (c *Capture) MustOne(t TB, msg string) Record

MustOne returns the single record with Message == msg, failing the test if zero or multiple match.

func (*Capture) Records

func (c *Capture) Records() []Record

Records returns a copy of the captured records.

func (*Capture) Reset

func (c *Capture) Reset()

Reset clears the recorded log.

func (*Capture) WithAttrs

func (c *Capture) WithAttrs(attrs []slog.Attr) slog.Handler

WithAttrs returns a child handler that prepends attrs to every record.

func (*Capture) WithGroup

func (c *Capture) WithGroup(name string) slog.Handler

WithGroup returns a child handler that nests subsequent attrs under name.

func (*Capture) WithMessage

func (c *Capture) WithMessage(msg string) []Record

WithMessage returns the subset of records whose Message equals msg.

type Record

type Record struct {
	Level   slog.Level
	Message string
	Attrs   map[string]any
}

Record is a captured slog entry with attributes flattened to a dotted-key map (groups become "g.k").

func (Record) Bool

func (r Record) Bool(key string) bool

Bool returns the attribute as a bool. Returns false if absent or non-bool.

func (Record) Int

func (r Record) Int(key string) int

Int returns the attribute as an int. Returns 0 if absent or not numeric.

func (Record) String

func (r Record) String(key string) string

String returns the attribute as a string (using fmt %v if not a string). Returns "" if the key is absent.

type TB

type TB interface {
	Errorf(format string, args ...any)
	Helper()
}

TB is the subset of testing.TB used by Assert helpers. We avoid pulling in *testing.T so the package can be exercised under stub Ts in its own unit tests.

Jump to

Keyboard shortcuts

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