logtest

package
v9.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2026 License: Apache-2.0 Imports: 4 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package logtest provides test utilities for slog-based logging.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Entry

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

Entry represents a captured log record.

type MockHandler

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

MockHandler is a slog.Handler that captures log records for test assertions. It is safe for concurrent use. Entries are shared across handlers created via WithAttrs/WithGroup so that the original handler can observe all log records.

func NewMockHandler

func NewMockHandler() *MockHandler

NewMockHandler creates a new MockHandler that captures all log records at Debug level and above.

func NewMockLogger

func NewMockLogger() (*slog.Logger, *MockHandler)

NewMockLogger returns a *slog.Logger backed by a MockHandler, plus the handler for inspection.

func (*MockHandler) AllMessages

func (h *MockHandler) AllMessages() []string

AllMessages returns all captured messages across all levels.

func (*MockHandler) Enabled

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

func (*MockHandler) Entries

func (h *MockHandler) Entries() []Entry

Entries returns a copy of all captured log entries.

func (*MockHandler) EntriesForLevel

func (h *MockHandler) EntriesForLevel(level slog.Level) []Entry

EntriesForLevel returns captured entries at the specified level.

func (*MockHandler) Handle

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

func (*MockHandler) HasMessage

func (h *MockHandler) HasMessage(level slog.Level, substr string) bool

HasMessage returns true if any captured entry at the given level contains the substring.

func (*MockHandler) Messages

func (h *MockHandler) Messages(level slog.Level) []string

Messages returns all captured messages at the specified level.

func (*MockHandler) Reset

func (h *MockHandler) Reset()

Reset clears all captured entries.

func (*MockHandler) SetLevel

func (h *MockHandler) SetLevel(level slog.Level)

SetLevel sets the minimum log level for the handler.

func (*MockHandler) WithAttrs

func (h *MockHandler) WithAttrs(attrs []slog.Attr) slog.Handler

func (*MockHandler) WithGroup

func (h *MockHandler) WithGroup(name string) slog.Handler

Jump to

Keyboard shortcuts

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