testlog

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 21, 2025 License: Unlicense Imports: 4 Imported by: 0

Documentation

Overview

Package testlog provides control over logging during tests.

Log messages written to a test case's log using t.Logf are only printed if that particular test fails. However, if code under test logs to a log.Logger, slog.Logger, or the defult Loggers via functions in packages log or slog, any test failure in the package will cause all log messages for the package to be printed – even messages from passing tests. This is very confusing.

To avoid that, this package allows test cases to get a Logger or override the standard logger in a way that redirects logging output into the test log. This way, only logs for failing tests appear in test output.

Based on code from here: https://github.com/golang/go/issues/22513

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Logger

func Logger(t testing.TB) *log.Logger

Logger returns a log.Logger that writes to t.Log.

func Override

func Override(t testing.TB)

Override redirects the output of the default loggers to call t.Log. When t and all of its subtests are complete, the original log output is restored.

func Slogger

func Slogger(t testing.TB) *slog.Logger

Slogger returns a slog.Logger that writes to t.Log.

func Tee

func Tee(t testing.TB, w io.Writer)

Tee, like Override, redirects the output of the default loggers to call t.Log. A copy of all log messages is also written to w. When t and all of its subtests are complete, the original log output is restored.

Types

type Writer

type Writer struct {
	TB testing.TB
}

Writer treats a test case as an io.Writer, passing any output to the case's Log method.

func (Writer) Write

func (w Writer) Write(p []byte) (n int, err error)

Jump to

Keyboard shortcuts

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