Documentation
¶
Overview ¶
Package logtest provides test utilities for slog-based logging.
Index ¶
- type Entry
- type MockHandler
- func (h *MockHandler) AllMessages() []string
- func (h *MockHandler) Enabled(_ context.Context, level slog.Level) bool
- func (h *MockHandler) Entries() []Entry
- func (h *MockHandler) EntriesForLevel(level slog.Level) []Entry
- func (h *MockHandler) Handle(_ context.Context, r slog.Record) error
- func (h *MockHandler) HasMessage(level slog.Level, substr string) bool
- func (h *MockHandler) Messages(level slog.Level) []string
- func (h *MockHandler) Reset()
- func (h *MockHandler) SetLevel(level slog.Level)
- func (h *MockHandler) WithAttrs(attrs []slog.Attr) slog.Handler
- func (h *MockHandler) WithGroup(name string) slog.Handler
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type MockHandler ¶
type MockHandler struct {
// contains filtered or unexported fields
}
MockHandler is a slog.Handler that captures log records for test assertions. It is safe for concurrent use. Entries are shared across handlers created via WithAttrs/WithGroup so that the original handler can observe all log records.
func NewMockHandler ¶
func NewMockHandler() *MockHandler
NewMockHandler creates a new MockHandler that captures all log records at Debug level and above.
func NewMockLogger ¶
func NewMockLogger() (*slog.Logger, *MockHandler)
NewMockLogger returns a *slog.Logger backed by a MockHandler, plus the handler for inspection.
func (*MockHandler) AllMessages ¶
func (h *MockHandler) AllMessages() []string
AllMessages returns all captured messages across all levels.
func (*MockHandler) Entries ¶
func (h *MockHandler) Entries() []Entry
Entries returns a copy of all captured log entries.
func (*MockHandler) EntriesForLevel ¶
func (h *MockHandler) EntriesForLevel(level slog.Level) []Entry
EntriesForLevel returns captured entries at the specified level.
func (*MockHandler) HasMessage ¶
func (h *MockHandler) HasMessage(level slog.Level, substr string) bool
HasMessage returns true if any captured entry at the given level contains the substring.
func (*MockHandler) Messages ¶
func (h *MockHandler) Messages(level slog.Level) []string
Messages returns all captured messages at the specified level.
func (*MockHandler) SetLevel ¶
func (h *MockHandler) SetLevel(level slog.Level)
SetLevel sets the minimum log level for the handler.