collect

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2025 License: MIT Imports: 5 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

This section is empty.

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) 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) 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