loggertest

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Jul 26, 2026 License: MIT Imports: 7 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func FlatAttrs

func FlatAttrs(r slog.Record) map[string]any

FlatAttrs collapses a record's attributes into key→value, flattening groups with dotted keys (e.g. "http.method"). Keeps test assertions readable without having to manually walk slog.Value.Group() trees.

func PCFrame

func PCFrame(pc uintptr) runtime.Frame

PCFrame turns a single program counter into its first stack frame. Useful for asserting on the source attribute slog will derive from a record's PC.

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) Enabled

func (h *CaptureHandler) Enabled(_ context.Context, _ slog.Level) bool

func (*CaptureHandler) Find

func (h *CaptureHandler) Find(t *testing.T, msg string) slog.Record

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) Handle

func (h *CaptureHandler) Handle(_ context.Context, r slog.Record) error

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.

func (*CaptureHandler) WithAttrs

func (h *CaptureHandler) WithAttrs(_ []slog.Attr) slog.Handler

func (*CaptureHandler) WithGroup

func (h *CaptureHandler) WithGroup(_ string) slog.Handler

Jump to

Keyboard shortcuts

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