logtest

package
v0.0.0-...-517b696 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 23, 2025 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

logtest provides logger constructors to use in tests and utilities for testing logging.

Index

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 scoped to the given test, and returns a callback, exportLogs, which flushes the logger buffer and returns log entries.

The logged values are also printed to stderr in test output. The log level for stderr will be Debug if the tests are run in verbose mode, and Warn otherwise.

To suppress logging to stderr, use CapturedWith instead.

The returned callback exportLogs will return entries for all log levels.

func CapturedWith

func CapturedWith(t testing.TB, stderrOptions LoggerOptions) (logger log.Logger, exportLogs func() CapturedLogs)

CapturedWith generalizes Captured but allows customizing the logging options for stderr.

To suppress logging to stderr, use LoggerOptions{Level: log.LevelNone}.

Like Captured, the exportLogs callback still captures logs for every level.

func Init

func Init(_ *testing.M)

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

func InitWithLevel(_ *testing.M, level log.Level)

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 NoOp

func NoOp(_ *testing.T) log.Logger

NoOp returns a no-op Logger, useful for silencing all output in a specific test.

func Scoped

func Scoped(t testing.TB) log.Logger

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 CapturedLog struct {
	Time    time.Time
	Scope   string
	Level   log.Level
	Message string
	Fields  map[string]interface{}
}

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.

type LoggerOptions

type LoggerOptions struct {
	// Level configures the minimum log level to output.
	Level log.Level
	// FailOnErrorLogs indicates that the test should fail if an error log is output.
	FailOnErrorLogs bool
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL