Documentation ¶
Overview ¶
logtest provides logger constructors to use in tests and utilities for testing logging.
Index ¶
- func Captured(t testing.TB) (logger log.Logger, exportLogs func() CapturedLogs)
- func Init(_ *testing.M)
- func InitWithLevel(_ *testing.M, level log.Level)
- func NoOp(_ *testing.T) log.Logger
- func Scoped(t testing.TB) log.Logger
- func ScopedWith(t testing.TB, options LoggerOptions) log.Logger
- type CapturedLog
- type CapturedLogs
- type LoggerOptions
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Captured ¶
func Captured(t testing.TB) (logger log.Logger, exportLogs func() CapturedLogs)
Captured retrieves a logger from scoped to the the given test, and returns a callback, dumpLogs, which flushes the logger buffer and returns log entries.
func Init ¶
Init can be used to instantiate the log package for running tests, to be called in TestMain for the relevant package. Remember to call (*testing.M).Run() after initializing the logger!
testing.M is an unused argument, used to indicate this function should be called in TestMain.
func InitWithLevel ¶
InitWithLevel does the same thing as Init, but uses the provided log level to configur the log level for this package's tests, which can be helpful for exceptionally noisy tests.
If your loggers are parameterized, you can also use logtest.NoOp to silence output for specific tests.
func Scoped ¶
Scoped retrieves a logger scoped to the the given test. It writes to testing.TB.
Unlike log.Scoped(), logtest.Scoped() is safe to use without initialization.
func ScopedWith ¶
func ScopedWith(t testing.TB, options LoggerOptions) log.Logger
Scoped retrieves a logger scoped to the the given test, configured with additional options. It writes to testing.TB.
Unlike log.Scoped(), logtest.Scoped() is safe to use without initialization.
Types ¶
type CapturedLog ¶
type CapturedLogs ¶
type CapturedLogs []CapturedLog
func (CapturedLogs) Contains ¶
func (cl CapturedLogs) Contains(condition func(l CapturedLog) bool) bool
Contains asserts that at least one entry matching the condition exists in the captured logs.
func (CapturedLogs) Filter ¶
func (cl CapturedLogs) Filter(condition func(l CapturedLog) bool) CapturedLogs
Filter returns captured logs that match the condition.
func (CapturedLogs) Messages ¶
func (cl CapturedLogs) Messages() []string
Messages aggregates all messages (excluding fields) from the captured log entries.