testlogger

package
v1.29.2 Latest Latest
Warning

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

Go to latest
Published: Dec 18, 2025 License: MIT Imports: 16 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 int
const (
	Debug Level = iota
	Info
	Warn
	Error
	Fatal
	DPanic
	Panic
)

func (Level) String

func (i 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 WithoutCaller added in v1.29.0

func WithoutCaller() LoggerOption

WithoutCaller stops the TestLogger from logging the caller. This is primarily intended for use when parsing log.Logger-generated logs in files or coming from subprocesses.

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 DontFailOnDPanic added in v1.29.0

func DontFailOnDPanic(t *TestLogger) *TestLogger

func DontFailOnError added in v1.29.0

func DontFailOnError(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) FailOnDPanic added in v1.29.0

func (tl *TestLogger) FailOnDPanic(b bool) bool

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

func (*TestLogger) FailOnError added in v1.29.0

func (tl *TestLogger) FailOnError(b bool) bool

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

func (*TestLogger) FailOnFatal added in v1.29.0

func (tl *TestLogger) FailOnFatal(b bool) bool

FailOnFatal overrides the behavior of this logger. It returns the previous value so that it can be restored later. Note that Fatal-level logs still panic

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) 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