Documentation
¶
Overview ¶
Package testlog provides a log handler for unit tests.
Index ¶
- func Logger(t Testing, level slog.Level) log.Logger
- func LoggerWithHandlerMod(t Testing, level slog.Level, handlerMods ...logmods.HandlerMod) log.Logger
- func WrapCaptureLogger(h slog.Handler) slog.Handler
- type CapturedAttributes
- type CapturedRecord
- type Capturer
- type CapturingHandler
- func (c *CapturingHandler) Clear()
- func (c *CapturingHandler) Enabled(ctx context.Context, level slog.Level) bool
- func (c *CapturingHandler) FindLog(filters ...LogFilter) *CapturedRecord
- func (c *CapturingHandler) FindLogs(filters ...LogFilter) []*CapturedRecord
- func (c *CapturingHandler) Handle(ctx context.Context, r slog.Record) error
- func (c *CapturingHandler) RequireMessageContained(t require.TestingT, message string, filters ...LogFilter)
- func (c *CapturingHandler) RequireMessageContainedNTimes(t require.TestingT, message string, n int, filters ...LogFilter)
- func (c *CapturingHandler) RequireMessageContainedOnce(t require.TestingT, message string, filters ...LogFilter)
- func (c *CapturingHandler) Unwrap() slog.Handler
- func (c *CapturingHandler) WithAttrs(attrs []slog.Attr) slog.Handler
- func (c *CapturingHandler) WithGroup(name string) slog.Handler
- type LogFilter
- func NewAttributesContainsFilter(key, value string) LogFilter
- func NewAttributesFilter(key, value string) LogFilter
- func NewErrContainsFilter(errMessage string) LogFilter
- func NewLevelFilter(level slog.Level) LogFilter
- func NewMessageContainsFilter(message string) LogFilter
- func NewMessageFilter(message string) LogFilter
- type Testing
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func LoggerWithHandlerMod ¶
Types ¶
type CapturedAttributes ¶
type CapturedAttributes struct {
Parent *CapturedAttributes
Attributes []slog.Attr
}
CapturedAttributes forms a chain of inherited attributes, to traverse on captured log records.
func (*CapturedAttributes) Attrs ¶
func (r *CapturedAttributes) Attrs(f func(slog.Attr) bool)
Attrs calls f on each Attr in the CapturedAttributes. Iteration stops if f returns false.
type CapturedRecord ¶
type CapturedRecord struct {
Parent *CapturedAttributes
*slog.Record
}
CapturedRecord is a wrapped around a regular log-record, to preserve the inherited attributes context, without mutating the record or reordering attributes.
func (*CapturedRecord) AttrValue ¶
func (r *CapturedRecord) AttrValue(name string) (v any)
func (*CapturedRecord) Attrs ¶
func (r *CapturedRecord) Attrs(f func(slog.Attr) bool)
Attrs calls f on each Attr in the CapturedRecord. Iteration stops if f returns false.
type Capturer ¶
type Capturer interface {
slog.Handler
Clear()
FindLog(filters ...LogFilter) *CapturedRecord
FindLogs(filters ...LogFilter) []*CapturedRecord
}
type CapturingHandler ¶
type CapturingHandler struct {
Logs *[]*CapturedRecord // shared among derived CapturingHandlers
// contains filtered or unexported fields
}
CapturingHandler provides a log handler that captures all log records and optionally forwards them to a delegate. Note that it is not thread safe.
func CaptureLogger ¶
func (*CapturingHandler) Clear ¶
func (c *CapturingHandler) Clear()
func (*CapturingHandler) FindLog ¶
func (c *CapturingHandler) FindLog(filters ...LogFilter) *CapturedRecord
func (*CapturingHandler) FindLogs ¶
func (c *CapturingHandler) FindLogs(filters ...LogFilter) []*CapturedRecord
func (*CapturingHandler) RequireMessageContained ¶
func (c *CapturingHandler) RequireMessageContained(t require.TestingT, message string, filters ...LogFilter)
func (*CapturingHandler) RequireMessageContainedNTimes ¶
func (*CapturingHandler) RequireMessageContainedOnce ¶
func (c *CapturingHandler) RequireMessageContainedOnce(t require.TestingT, message string, filters ...LogFilter)
func (*CapturingHandler) Unwrap ¶
func (c *CapturingHandler) Unwrap() slog.Handler
type LogFilter ¶
type LogFilter func(record *CapturedRecord) bool
func NewAttributesFilter ¶
func NewErrContainsFilter ¶
func NewLevelFilter ¶
func NewMessageFilter ¶
type Testing ¶
type Testing interface {
Logf(format string, args ...any)
Helper()
FailNow()
Name() string
Cleanup(func())
}
Testing interface to log to. Some functions are marked as Helper function to log the call site accurately. Standard Go testing.TB implements this, as well as Hive and other Go-like test frameworks.