sloghelper

package
v0.1.0 Latest Latest
Warning

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

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

Documentation

Overview

Package sloghelper provides test helpers for asserting slog JSON output. It avoids false positives from full-string Contains/NotContains by parsing each log line individually and matching on specific message substrings.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FindLogEntry

func FindLogEntry(logOutput, msgSubstr string) map[string]any

FindLogEntry scans the JSON log output (one JSON object per line) and returns the first parsed line whose "msg" value contains msgSubstr. Returns nil if no matching line is found. Malformed JSON lines are silently skipped.

Typical use in tests:

entry := sloghelper.FindLogEntry(buf.String(), "session not found")
require.NotNil(t, entry, "expected a log line about session not found")
assert.Equal(t, "WARN", entry["level"])

Types

type SyncBuffer

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

SyncBuffer is a concurrency-safe slog output sink. A test that installs a slog handler over it from the main goroutine while a worker goroutine logs concurrently MUST use this instead of a bare bytes.Buffer — slog.Handler writes from the worker race with the test's String() read otherwise (the race the bare-buffer worker tests previously had under -race).

func NewSyncBuffer

func NewSyncBuffer() *SyncBuffer

NewSyncBuffer returns a ready-to-use SyncBuffer.

func (*SyncBuffer) String

func (b *SyncBuffer) String() string

String returns the accumulated log output. Safe to call while concurrent writes are in flight.

func (*SyncBuffer) Write

func (b *SyncBuffer) Write(p []byte) (int, error)

Write implements io.Writer.

Jump to

Keyboard shortcuts

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