slogutil

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2026 License: MIT Imports: 6 Imported by: ? 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 slogutil provides slog helpers that capture log records in tests.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Entry

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

Entry is a record captured by an observer.

func (Entry) ContextMap

func (e Entry) ContextMap() map[string]any

ContextMap returns the attributes of the record by key. An error is its message.

type GatedHandler

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

GatedHandler wraps an slog.Handler and blocks records whose message contains match until Release is called.

func NewGatedHandler

func NewGatedHandler(inner slog.Handler, match string) *GatedHandler

NewGatedHandler returns a GatedHandler that delegates to inner and blocks matching records until Release.

func (*GatedHandler) Enabled

Enabled implements slog.Handler. Always returns true so gated matching inspects every record even when inner discards.

func (*GatedHandler) Entered

func (g *GatedHandler) Entered() <-chan struct{}

Entered is closed when the first matching record enters Handle.

func (*GatedHandler) Handle

func (g *GatedHandler) Handle(ctx context.Context, r slog.Record) error

Handle implements slog.Handler.

func (*GatedHandler) Release

func (g *GatedHandler) Release()

Release unblocks all gated Handle calls. It is safe to call multiple times.

func (*GatedHandler) WithAttrs

func (g *GatedHandler) WithAttrs(attrs []slog.Attr) slog.Handler

WithAttrs implements slog.Handler.

func (*GatedHandler) WithGroup

func (g *GatedHandler) WithGroup(name string) slog.Handler

WithGroup implements slog.Handler.

type ObservedLogs

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

ObservedLogs are the records captured by an observer.

func NewObserver

func NewObserver(level slog.Level) (*slog.Logger, *ObservedLogs)

NewObserver returns a logger that keeps the records of the level and above, and the way to read them.

func (*ObservedLogs) All

func (o *ObservedLogs) All() []Entry

All returns the captured records.

func (*ObservedLogs) FilterMessage

func (o *ObservedLogs) FilterMessage(msg string) *ObservedLogs

FilterMessage returns the records with exactly this message.

func (*ObservedLogs) FilterMessageSnippet

func (o *ObservedLogs) FilterMessageSnippet(snippet string) *ObservedLogs

FilterMessageSnippet returns the records whose message contains the snippet.

func (*ObservedLogs) Len

func (o *ObservedLogs) Len() int

Len returns the number of captured records.

type PlainHandler

type PlainHandler struct {
	Writer *os.File
}

PlainHandler is a slog handler that writes each record as its message followed by its attributes (key=value), without the time and the level.

func (*PlainHandler) Enabled

func (*PlainHandler) Enabled(_ context.Context, _ slog.Level) bool

Enabled implements the slog.Handler interface.

func (*PlainHandler) Handle

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

Handle implements the slog.Handler interface.

func (*PlainHandler) WithAttrs

func (h *PlainHandler) WithAttrs(_ []slog.Attr) slog.Handler

WithAttrs implements the slog.Handler interface.

func (*PlainHandler) WithGroup

func (h *PlainHandler) WithGroup(_ string) slog.Handler

WithGroup implements the slog.Handler interface.

Jump to

Keyboard shortcuts

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