Documentation
¶
Overview ¶
Collecting log/slog handler, usefull for testing.
Index ¶
- func TestID(ctx context.Context) string
- func WithTestID(ctx context.Context, str string) context.Context
- type CollectorHandler
- func (h *CollectorHandler) All() []map[string]any
- func (h *CollectorHandler) CollectWith(names ...string) []any
- func (h *CollectorHandler) Contains(expected any, names ...string) bool
- func (h *CollectorHandler) Count() int
- func (h *CollectorHandler) CountWith(names ...string) int
- func (h *CollectorHandler) Enabled(ctx context.Context, level slog.Level) bool
- func (h *CollectorHandler) Handle(ctx context.Context, r slog.Record) error
- func (h *CollectorHandler) Last() map[string]any
- func (h *CollectorHandler) Reset()
- func (h *CollectorHandler) String() string
- func (h *CollectorHandler) WithAttrs(attrs []slog.Attr) slog.Handler
- func (h *CollectorHandler) WithGroup(name string) slog.Handler
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
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) 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.