logtest

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2026 License: MIT Imports: 5 Imported by: 0

README

logtest

import "github.com/brpaz/lib-go/logging/logtest"

Package logtest provides test helpers for the log package.

Use NewRecorder to capture structured log output and assert on individual fields without timestamp noise or JSON parsing boilerplate:

rec := logtest.NewRecorder(t)
logger, _ := log.NewLogger(log.WithOutput(rec), log.WithFormat(log.FormatJSON))

logger.Info(ctx, "user created", slog.String("user_id", "u-1"))

entry := rec.Last()
assert.Equal(t, "user created", entry["message"])
assert.Equal(t, "u-1", entry["user_id"])

Index

type Recorder

Recorder captures log output written by a log.Logger configured with log.FormatJSON and log.WithOutput(rec). Each written record is parsed into a map with the "timestamp" key stripped for stable assertions.

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

func New
func New(t *testing.T) (*logging.Logger, *Recorder)

New returns a [logging.Logger] and a Recorder pre-configured for testing. The logger writes JSON at DEBUG level so all records are captured.

func NewRecorder
func NewRecorder(t *testing.T) *Recorder

NewRecorder returns a Recorder ready for use in t.

func (*Recorder) All
func (r *Recorder) All() []map[string]any

All returns a copy of every captured record in order.

func (*Recorder) Last
func (r *Recorder) Last() map[string]any

Last returns the most recent captured record. Calls t.Fatal if no records have been captured yet.

func (*Recorder) Len
func (r *Recorder) Len() int

Len returns the number of captured records.

func (*Recorder) Reset
func (r *Recorder) Reset()

Reset discards all captured records.

func (*Recorder) Write
func (r *Recorder) Write(p []byte) (int, error)

Write implements io.Writer. Each call is expected to contain exactly one complete JSON log record (the slog JSON handler guarantees this).

Generated by gomarkdoc

Documentation

Overview

Package logtest provides test helpers for the log package.

Use NewRecorder to capture structured log output and assert on individual fields without timestamp noise or JSON parsing boilerplate:

rec := logtest.NewRecorder(t)
logger, _ := log.NewLogger(log.WithOutput(rec), log.WithFormat(log.FormatJSON))

logger.Info(ctx, "user created", slog.String("user_id", "u-1"))

entry := rec.Last()
assert.Equal(t, "user created", entry["message"])
assert.Equal(t, "u-1", entry["user_id"])

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 captures log output written by a log.Logger configured with log.FormatJSON and log.WithOutput(rec). Each written record is parsed into a map with the "timestamp" key stripped for stable assertions.

func New

func New(t *testing.T) (*logging.Logger, *Recorder)

New returns a logging.Logger and a Recorder pre-configured for testing. The logger writes JSON at DEBUG level so all records are captured.

func NewRecorder

func NewRecorder(t *testing.T) *Recorder

NewRecorder returns a Recorder ready for use in t.

func (*Recorder) All

func (r *Recorder) All() []map[string]any

All returns a copy of every captured record in order.

func (*Recorder) Last

func (r *Recorder) Last() map[string]any

Last returns the most recent captured record. Calls t.Fatal if no records have been captured yet.

func (*Recorder) Len

func (r *Recorder) Len() int

Len returns the number of captured records.

func (*Recorder) Reset

func (r *Recorder) Reset()

Reset discards all captured records.

func (*Recorder) Write

func (r *Recorder) Write(p []byte) (int, error)

Write implements io.Writer. Each call is expected to contain exactly one complete JSON log record (the slog JSON handler guarantees this).

Jump to

Keyboard shortcuts

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