collect

package
v0.0.8 Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2025 License: MIT Imports: 8 Imported by: 0

README

collect

A collector handler - a special slog handler used for testing. Usage:

ch := collect.NewTestHandler(slog.LevelDebug, false, false, false)
logger := slog.New(ch)
logger.Debug("message")
logger.Info("message")

fmt.Printf("%v\n", ch.All())
fmt.Printf("%v\n", ch.Last())

Logs are collected into a slice of map[string]any where they can be picked up for introspection. This package has no other use than in testing.

Documentation

Overview

Collecting log/slog handler, usefull for testing.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func TestID added in v0.0.5

func TestID(ctx context.Context) string

func WithTestID added in v0.0.5

func WithTestID(ctx context.Context, str string) context.Context

Types

type CollectorHandler

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

CollectorHandler is a test handler that buffers log entries. This is useful for testing of log output.

func NewTestHandler

func NewTestHandler(level slog.Level, addTime, addSource, addLevel bool) *CollectorHandler

NewTestHandler creates a new BufferHandler.

func (*CollectorHandler) All

func (h *CollectorHandler) All() []map[string]any

All returns all entries that were logged.

func (*CollectorHandler) CollectWith added in v0.0.8

func (h *CollectorHandler) CollectWith(names ...string) []any

CollectWith collects values for the provided nested names. It supports nested maps of fields (slog groups).

func (*CollectorHandler) Contains added in v0.0.5

func (h *CollectorHandler) Contains(expected any, names ...string) bool

Contains searches through all collected logs for expected value. It supports nested maps of fields (slog groups), and returns true if the value matches expected value. Native slog fields like message, time or level are also supported.

Example: a record created with logger.WithGroup("g").Debug("test", "key", "value") will be a match for call Contains("value", "g", "key").

func (*CollectorHandler) Count added in v0.0.6

func (h *CollectorHandler) Count() int

Count returns number of all entries that were logged.

func (*CollectorHandler) CountWith added in v0.0.8

func (h *CollectorHandler) CountWith(names ...string) int

CountWith counts how many records contain the provided nested names. It supports nested maps of fields (slog groups).

func (*CollectorHandler) Enabled

func (h *CollectorHandler) Enabled(ctx context.Context, level slog.Level) bool

func (*CollectorHandler) Handle

func (h *CollectorHandler) Handle(ctx context.Context, r slog.Record) error

func (*CollectorHandler) Last

func (h *CollectorHandler) Last() map[string]any

Last returns the last entry that was logged or an empty map

func (*CollectorHandler) Reset

func (h *CollectorHandler) Reset()

Reset removes all Entries from this test hook.

func (*CollectorHandler) String added in v0.0.4

func (h *CollectorHandler) String() string

String returns all records formatted as a string.

func (*CollectorHandler) WithAttrs

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

func (*CollectorHandler) WithGroup

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

Jump to

Keyboard shortcuts

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