logptest

package
v0.26.2 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2025 License: Apache-2.0 Imports: 16 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewTestingLogger

func NewTestingLogger(t testing.TB, selector string, options ...logp.LogOption) *logp.Logger

NewTestingLogger returns a testing suitable logp.Logger that uses the testing.T as the logger output.

func NewTestingLoggerWithObserver added in v0.21.6

func NewTestingLoggerWithObserver(t testing.TB, selector string) (*logp.Logger, *observer.ObservedLogs)

NewTestingLoggerWithObserver returns a testing suitable logp.Logger that uses the testing.T as the logger output and an observer.

Types

type Logger added in v0.25.0

type Logger struct {
	*logp.Logger
	// contains filtered or unexported fields
}

Logger wraps a *logp.Logger and makes it more suitable for tests. Key features:

  • All logs are saved on a single temporary log file
  • On failures, the log file is kept and its path printed
  • Methods to search and wait for log entries are provided, they keep track of the offset, ensuring ordering when when searching for logs

func NewFileLogger added in v0.25.0

func NewFileLogger(t testing.TB, dir string) *Logger

NewFileLogger returns a logger that logs to a file and has methods to search in the logs. If dir is not an empty string, the log file will be generated on this folder. This is useful to make CI collect the logs in case a test fails. If dir is an empty string, the OS temporary folder is used.

The *logp.Logger is embedded into it, so Logger is a drop-in replacement for a *logp.Logger, or the logger can be accessed via [Logger.Logger]

func (*Logger) FindInLogs added in v0.25.0

func (l *Logger) FindInLogs(str string) (bool, error)

FindInLogs searches for str in the log file keeping track of the offset. It returns true if str is found in the logs. If there are any errors, it returns false and the error

func (*Logger) LogContains added in v0.25.0

func (l *Logger) LogContains(t testing.TB, str string)

LogContains searches for str in the log file keeping track of the offset. If there is any issue reading the log file, then t.Fatalf is called, if str is not present in the logs, t.Errorf is called.

func (*Logger) ResetOffset added in v0.25.0

func (l *Logger) ResetOffset()

ResetOffset resets the log file offset

func (*Logger) WaitLogsContains added in v0.25.0

func (l *Logger) WaitLogsContains(t testing.TB, s string, timeout time.Duration, msgAndArgs ...any)

WaitLogsContains waits for the specified string s to be present in the logs within the given timeout duration and fails the test if s is not found. It keeps track of the log file offset, reading only new lines. Each subsequent call to WaitLogsContains will only check logs not yet evaluated. msgAndArgs should be a format string and arguments that will be printed if the logs are not found, providing additional context for debugging.

Jump to

Keyboard shortcuts

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