testlogger

package
v1.28.3 Latest Latest
Warning

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

Go to latest
Published: Mar 17, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CleanupCapableT

type CleanupCapableT interface {
	TestingT
	Cleanup(func())
}

CleanupCapable is an interface that allows a test to register cleanup functions. This is /not/ implemented by *rapid.T but is by *testing.T

type Expectation

type Expectation struct {
	// contains filtered or unexported fields
}

Expectations represent errors we expect to happen in tests. Their only purpose is to allow un-expecting (hah!) an error we've marked as expected

func (*Expectation) Forget

func (e *Expectation) Forget()

Forget removes a previously registered expectation. A forgotten expectation will no longer be evaluated when errors are encountered.

type Level

type Level string
const (
	Debug Level = "debug"
	Info  Level = "info"
	Warn  Level = "warn"
	// Panics, DPanics, and Fatal logs are considered errors
	Error Level = "error"
)

func (Level) String

func (l Level) String() string

type LoggerOption

type LoggerOption func(*TestLogger)

func LogLevel

func LogLevel(level zapcore.Level) LoggerOption

func SetLogLevel

func SetLogLevel(tt CleanupCapableT, level zapcore.Level) LoggerOption

SetLogLevel overrides the temporal test log level during this test.

func WithLogTags

func WithLogTags(tags ...tag.Tag) LoggerOption

WithLogTags adds tags to the logs emitted by the underlying logger

func WrapLogger

func WrapLogger(l log.Logger) LoggerOption

type Mode

type Mode string
const (
	// FailOnAnyUnexpectedError mode will fail if any unexpected error is encountered,
	// like an allowlist. Use TestLogger.Expect to add an error to the allowlist.
	FailOnAnyUnexpectedError Mode = "fail-on-unexpected-errors"
	// FailOnExpectedErrorOnly mode will only fail if an expected error is encountered,
	// like a blocklist. Use TestLogger.Expect to add an error to the blocklist.
	FailOnExpectedErrorOnly = "fail-on-expected-errors"
)

type TestLogger

type TestLogger struct {
	// contains filtered or unexported fields
}

TestLogger is a log.Logger implementation that logs to the test's logger _but_ will fail the test if log levels _above_ Warn are present

func DontPanicOnDPanic

func DontPanicOnDPanic(t *TestLogger) *TestLogger

func DontPanicOnError

func DontPanicOnError(t *TestLogger) *TestLogger

func LogExpectations

func LogExpectations(t *TestLogger) *TestLogger

func NewTestLogger

func NewTestLogger(t TestingT, mode Mode, opts ...LoggerOption) *TestLogger

NewTestLogger creates a new TestLogger that logs to the provided testing.T. Mode controls the behavior of the logger for when an expected or unexpected error is encountered.

func (*TestLogger) Close

func (tl *TestLogger) Close()

Close disallows any further logging, preventing the test framework from complaining about logging post-test.

func (*TestLogger) DPanic

func (tl *TestLogger) DPanic(msg string, tags ...tag.Tag)

DPanic implements log.Logger.

func (*TestLogger) Debug

func (tl *TestLogger) Debug(msg string, tags ...tag.Tag)

Debug implements log.Logger.

func (*TestLogger) Error

func (tl *TestLogger) Error(msg string, tags ...tag.Tag)

Error implements log.Logger.

func (*TestLogger) Expect

func (tl *TestLogger) Expect(level Level, msg string, tags ...tag.Tag) *Expectation

Expect instructs the logger to expect certain errors, as specified by the msg and tag arguments. Depending on the Mode of the test logger, the expectation either acts as an entry in a blocklist (FailOnExpectedErrorOnly) or an allowlist (FailOnAnyUnexpectedError).

func (*TestLogger) Fatal

func (tl *TestLogger) Fatal(msg string, tags ...tag.Tag)

Fatal implements log.Logger.

func (*TestLogger) Forget

func (tl *TestLogger) Forget(e *Expectation)

Forget removes a previously registered expectation. A forgotten expectation will no longer be evaluated when errors are encountered.

func (*TestLogger) Info

func (tl *TestLogger) Info(msg string, tags ...tag.Tag)

Info implements log.Logger.

func (*TestLogger) Panic

func (tl *TestLogger) Panic(msg string, tags ...tag.Tag)

Panic implements log.Logger.

func (*TestLogger) PanicOnError

func (tl *TestLogger) PanicOnError(v bool) bool

PanicOnError overrides the behavior of this logger. It returns the previous value so that it can be restored later.

func (*TestLogger) ResetFailureStatus

func (tl *TestLogger) ResetFailureStatus() bool

ResetFailureStatus resets the failure state, returning the previous value. This is useful to verify that no unexpected errors were logged after the test completed (together with DontPanicOnError and/or DontPanicOnDPanic).

func (*TestLogger) T

func (tl *TestLogger) T() TestingT

func (*TestLogger) Warn

func (tl *TestLogger) Warn(msg string, tags ...tag.Tag)

Warn implements log.Logger.

func (*TestLogger) With

func (tl *TestLogger) With(tags ...tag.Tag) log.Logger

With implements log.WithLogger

func (*TestLogger) WithTags

func (tl *TestLogger) WithTags(tags ...tag.Tag) *TestLogger

WithTags gives you a new logger, copying the tags of the source, appending the provided new Tags

type TestingT

type TestingT interface {
	zaptest.TestingT
	Helper()
	Name() string
	Logf(format string, args ...any)
	Log(args ...any)
	Fatalf(format string, args ...any)
	Fatal(args ...any)
}

This is a subset of the testing.T interface that we use in this package that is also shared with *rapid.T.

Jump to

Keyboard shortcuts

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