logrec

package module
v0.0.0-...-2fe9c72 Latest Latest
Warning

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

Go to latest
Published: Jan 23, 2026 License: MIT Imports: 7 Imported by: 0

README

logrec

Go Reference

Package logrec provides a slog.Handler implementation for recording logs during tests.

Installation

go get github.com/tenntenn/logrec

Usage

var rec *logrec.Recorder

func TestMain(m *testing.M) {
    rec = logrec.New()
    slog.SetDefault(slog.New(rec.Handler()))
    os.Exit(m.Run())
}

func TestSomething(t *testing.T) {
    tid := testid.New(t)
    ctx := testid.WithValue(t.Context(), tid)
    rec.Start(t, ctx)

    slog.InfoContext(ctx, "message", "key", "value")

    for record := range rec.Result(ctx).All() {
        // inspect record
    }
}

Features

  • Record logs per test ID for independent parallel test execution
  • Implements slog.Handler interface with full WithAttrs and WithGroup support
  • Automatic cleanup via t.Cleanup
  • Compatible with testing/slogtest

Requirements

  • Go 1.25.0 or later

License

MIT

Documentation

Overview

Package logrec provides a slog.Handler implementation for recording logs during tests.

Usage:

var rec *logrec.Recorder

func TestMain(m *testing.M) {
    rec = logrec.New()
    slog.SetDefault(slog.New(rec.Handler()))
    os.Exit(m.Run())
}

func TestSomething(t *testing.T) {
    tid := testid.New(t)
    ctx := testid.WithValue(t.Context(), tid)
    rec.Start(t, ctx)

    slog.InfoContext(ctx, "message", "key", "value")

    for record := range rec.Result(ctx).All() {
        // inspect record
    }
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Recorder

type Recorder struct {
	// contains filtered or unexported fields
}

Recorder manages log records per test ID (tid). It allows parallel tests to record their logs independently. Use New to create a Recorder, Recorder.Handler to get a slog.Handler, Recorder.Start to begin recording, and Recorder.Result to retrieve the recorded logs.

func New

func New() *Recorder

New creates a new Recorder.

func (*Recorder) Handler

func (r *Recorder) Handler() slog.Handler

Handler returns a slog.Handler that records logs to this Recorder.

func (*Recorder) Result

func (r *Recorder) Result(ctx context.Context) *Result

Result returns the recorded logs for the tid extracted from ctx. The returned Result contains a snapshot of the logs at the time of the call, making it safe to use even while other goroutines continue logging.

func (*Recorder) Start

func (r *Recorder) Start(t testing.TB, ctx context.Context)

Start begins recording logs for the tid extracted from ctx. The recorded logs are automatically cleaned up via t.Cleanup.

type Result

type Result struct {
	// contains filtered or unexported fields
}

Result holds the recorded log entries for a specific test ID. Use Result.All to iterate over the recorded logs.

func (*Result) All

func (res *Result) All() iter.Seq[slog.Record]

All returns an iterator over the recorded logs.

Jump to

Keyboard shortcuts

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