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 ¶
- Variables
- type Log
- func (l *Log) Debug(a ...interface{})
- func (l *Log) Debugf(format string, a ...interface{})
- func (l *Log) Error(a ...interface{})
- func (l *Log) Errorf(format string, v ...interface{})
- func (l *Log) Strict(strict bool) *Log
- func (l *Log) Warn(a ...interface{})
- func (l *Log) WarnIfCloseError(c io.Closer)
- func (l *Log) WarnIfError(err error)
- func (l *Log) WarnIfFuncError(fn func() error)
- func (l *Log) Warnf(format string, a ...interface{})
Constants ¶
This section is empty.
Variables ¶
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 NewWith ¶
NewWith returns a Log that pipes output to t, using the backing lg.Log instances returned by factoryFn to generate log messages.
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 ¶
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 ¶
Strict sets strict mode. When in strict mode, Errorf logs via t.Error instead of t.Log, thus resulting in test failure.