loggingtest

package
v1.15.0 Latest Latest
Warning

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

Go to latest
Published: Sep 8, 2026 License: BSD-3-Clause Imports: 7 Imported by: 0

Documentation

Overview

Package loggingtest provides logging.Logger implementations for use in tests. New forwards logs to a testing.TB, treating warnings and errors as test failures. NewRecorder captures logs in memory so tests can assert on what was logged without any output.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Logger

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

Logger is a logging.Logger that propagates logs to testing.TB.

func New

func New(tb testing.TB, level logging.Level) *Logger

New constructs a logger that propagates logs to testing.TB. WARNING and ERROR logs are sent to testing.TB.Errorf while FATAL is sent to testing.TB.Fatalf. All other logs are sent to testing.TB.Logf. Although the level can be configured, it is silently capped at logging.Warn.

func (*Logger) CancelOnError

func (l *Logger) CancelOnError(ctx context.Context) context.Context

CancelOnError pipes `ctx` to and from context.WithCancel, calling the context.CancelFunc after logs >= logging.Error, and during testing.TB cleanup.

func (Logger) Debug

func (l Logger) Debug(msg string, fs ...zap.Field)

func (Logger) Error

func (l Logger) Error(msg string, fs ...zap.Field)

func (Logger) Fatal

func (l Logger) Fatal(msg string, fs ...zap.Field)

func (Logger) Info

func (l Logger) Info(msg string, fs ...zap.Field)

func (Logger) Trace

func (l Logger) Trace(msg string, fs ...zap.Field)

func (Logger) Warn

func (l Logger) Warn(msg string, fs ...zap.Field)

func (Logger) With

func (l Logger) With(fields ...zap.Field) logging.Logger

type Record

type Record struct {
	Level  logging.Level
	Msg    string
	Fields []zap.Field
}

A Record is a single entry in a Recorder.

type Recorder

type Recorder struct {
	Records []*Record
	// contains filtered or unexported fields
}

A Recorder is a logging.Logger that stores all logs as Record entries for inspection.

func NewRecorder

func NewRecorder(level logging.Level) *Recorder

NewRecorder constructs a new Recorder at the specified level.

func (*Recorder) At

func (l *Recorder) At(lvl logging.Level) []*Record

At returns all recorded logs at the specified logging.Level.

func (*Recorder) AtLeast

func (l *Recorder) AtLeast(lvl logging.Level) []*Record

AtLeast returns all recorded logs at or above the specified logging.Level.

func (Recorder) Debug

func (l Recorder) Debug(msg string, fs ...zap.Field)

func (Recorder) Error

func (l Recorder) Error(msg string, fs ...zap.Field)

func (Recorder) Fatal

func (l Recorder) Fatal(msg string, fs ...zap.Field)

func (*Recorder) Filter

func (l *Recorder) Filter(fn func(*Record) bool) []*Record

Filter returns the recorded logs for which `fn` returns true.

func (Recorder) Info

func (l Recorder) Info(msg string, fs ...zap.Field)

func (Recorder) Trace

func (l Recorder) Trace(msg string, fs ...zap.Field)

func (Recorder) Warn

func (l Recorder) Warn(msg string, fs ...zap.Field)

func (Recorder) With

func (l Recorder) With(fields ...zap.Field) logging.Logger

Jump to

Keyboard shortcuts

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