Documentation
¶
Overview ¶
Package test provides helpers shared across the test suites, such as a logger that captures records for assertions.
Index ¶
- func NewTestLogger() *slog.Logger
- type Handler
- func (h *Handler) Clear()
- func (h *Handler) Enabled(_ context.Context, level slog.Level) bool
- func (h *Handler) ErrorCount() int
- func (h *Handler) GetMessages() []LogEntry
- func (h *Handler) Handle(_ context.Context, r slog.Record) error
- func (h *Handler) HasError(substr string) bool
- func (h *Handler) HasMessage(substr string) bool
- func (h *Handler) HasWarning(substr string) bool
- func (h *Handler) MessageCount() int
- func (h *Handler) WarningCount() int
- func (h *Handler) WithAttrs(_ []slog.Attr) slog.Handler
- func (h *Handler) WithGroup(_ string) slog.Handler
- type LogEntry
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewTestLogger ¶
NewTestLogger creates a *slog.Logger backed by a capturing handler. The returned logger can be passed anywhere a *slog.Logger is expected. If you need to inspect captured log messages, use NewTestLoggerWithHandler instead.
Types ¶
type Handler ¶ added in v0.20.0
type Handler struct {
// contains filtered or unexported fields
}
Handler is a slog.Handler that captures log records for test assertions.
func NewHandler ¶ added in v0.20.0
func NewHandler() *Handler
NewHandler creates a new test handler that captures all log levels.
func NewTestLoggerWithHandler ¶ added in v0.20.0
NewTestLoggerWithHandler creates a *slog.Logger and returns both the logger and its Handler, allowing tests to inspect captured log messages.
func (*Handler) Clear ¶ added in v0.20.0
func (h *Handler) Clear()
Clear clears all captured messages.
func (*Handler) ErrorCount ¶ added in v0.20.0
ErrorCount returns the number of error messages.
func (*Handler) GetMessages ¶ added in v0.20.0
GetMessages returns all logged messages.
func (*Handler) HasError ¶ added in v0.20.0
HasError checks if an error message containing substr was logged.
func (*Handler) HasMessage ¶ added in v0.20.0
HasMessage checks if any message containing substr was logged.
func (*Handler) HasWarning ¶ added in v0.20.0
HasWarning checks if a warning message containing substr was logged.
func (*Handler) MessageCount ¶ added in v0.20.0
MessageCount returns the total number of captured messages.
func (*Handler) WarningCount ¶ added in v0.20.0
WarningCount returns the number of warning messages.