Documentation
¶
Overview ¶
Package slogutil provides slog helpers that capture log records in tests.
Index ¶
- type Entry
- type GatedHandler
- func (*GatedHandler) Enabled(context.Context, slog.Level) bool
- func (g *GatedHandler) Entered() <-chan struct{}
- func (g *GatedHandler) Handle(ctx context.Context, r slog.Record) error
- func (g *GatedHandler) Release()
- func (g *GatedHandler) WithAttrs(attrs []slog.Attr) slog.Handler
- func (g *GatedHandler) WithGroup(name string) slog.Handler
- type ObservedLogs
- type PlainHandler
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Entry ¶
Entry is a record captured by an observer.
func (Entry) ContextMap ¶
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 ¶
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) 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 ¶
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 ¶
Enabled implements the slog.Handler interface.
func (*PlainHandler) Handle ¶
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.