testlg

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2022 License: MIT Imports: 7 Imported by: 1

Documentation

Overview

Package testlg implements a lg.Log that directs output to the testing framework.

This is useful if your code under test writes to a log, and you want to capture that log output under testing.T. For example:

func TestMe(t *testing.T) {
  log := testlg.New(t)
  log.Debugf("Hello %s", "World")
  log.Warn("Hello Mars")
  log.Error("Hello Venus")
}

produces the following:

=== RUN   TestMe
--- PASS: TestMe (0.00s)
    testlg_test.go:64: 09:48:38.849066 	DEBUG	Hello World
    testlg_test.go:65: 09:48:38.849215 	WARN 	Hello Mars
    testlg_test.go:66: 09:48:38.849304 	ERROR	Hello Venus

Log has a "strict" mode which pipes Error and Errorf output to t.Error instead of t.Log, resulting in test failure. This:

func TestMe(t *testing.T) {
  log := testlg.New(t).Strict(true)
  log.Debug("Hello World")
  log.Warn("Hello Mars")
  log.Error("Hello Venus") // pipes to t.Error, resulting in test failure
}

produces:

=== RUN   TestMe
--- FAIL: TestMe (0.00s)
    testlg_test.go:64: 09:52:28.706482 	DEBUG	Hello World
    testlg_test.go:65: 09:52:28.706591 	WARN 	Hello Mars
    testlg_test.go:66: 09:52:28.706599 	ERROR	Hello Venus

This Log type does not itself generate log messages: this is delegated to a backing log impl (zaplg by default). An alternative impl can be set by passing a log factory func to NewWith, or by changing the testlg.FactoryFn package variable.

Index

Constants

This section is empty.

Variables

View Source
var FactoryFn = zaplg.TestingFactoryFn //nolint:gochecknoglobals // needed

FactoryFn is used by New to create the backing Log impl. By default this func uses zaplg, but other impls can be used as follows:

// Use loglg as the log implementation.
testlg.FactoryFn = func(w io.Writer) lg.Log {
  return loglg.NewWith(w, true, true, false)
}

Functions

This section is empty.

Types

type Log

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

Log implements lg.Log, but directs its output to the logging functions of testing.T.

func New

func New(t testing.TB) *Log

New returns a log that pipes output to t.

func NewWith

func NewWith(t testing.TB, factoryFn func(io.Writer) lg.Log) *Log

NewWith returns a Log that pipes output to t, using the backing lg.Log instances returned by factoryFn to generate log messages.

func (*Log) Debug

func (l *Log) Debug(a ...interface{})

Debug logs at DEBUG level to t.Log.

func (*Log) Debugf

func (l *Log) Debugf(format string, a ...interface{})

Debugf logs at DEBUG level to t.Log.

func (*Log) Error

func (l *Log) Error(a ...interface{})

Error logs at ERROR level to t.Log, or if in strict mode, the message is logged via t.Error, resulting in test failure.

func (*Log) Errorf

func (l *Log) Errorf(format string, v ...interface{})

Errorf logs at ERROR level to t.Log, or if in strict mode, the message is logged via t.Error, resulting in test failure.

func (*Log) Strict

func (l *Log) Strict(strict bool) *Log

Strict sets strict mode. When in strict mode, Errorf logs via t.Error instead of t.Log, thus resulting in test failure.

func (*Log) Warn

func (l *Log) Warn(a ...interface{})

Warn logs at WARN level to t.Log.

func (*Log) WarnIfCloseError

func (l *Log) WarnIfCloseError(c io.Closer)

func (*Log) WarnIfError

func (l *Log) WarnIfError(err error)

func (*Log) WarnIfFuncError

func (l *Log) WarnIfFuncError(fn func() error)

func (*Log) Warnf

func (l *Log) Warnf(format string, a ...interface{})

Warnf logs at WARN level to t.Log.

Jump to

Keyboard shortcuts

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