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 ¶
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.