testlog

package
v0.0.39 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2021 License: BSD-3-Clause Imports: 7 Imported by: 0

Documentation

Overview

Package testlog provides a test logger and helpers to check log output.

Example
package main

import (
	"testing"

	"github.com/heroku/x/testing/testlog"
)

func main() {
	var t *testing.T // use t provided by test function

	logger, hook := testlog.New()

	// Use the test logger
	logger.WithField("a", "b").Info("test")

	hook.CheckAllContained(t, "a=b", "msg=test")
	hook.CheckNotContained(t, "unexpected")
}
Output:

Example (Discard)
package main

import (
	"github.com/heroku/x/testing/testlog"
)

func main() {
	logger, _ := testlog.New()

	// Use the test logger
	logger.WithField("a", "b").Info("test")

}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Hook

type Hook struct {
	sync.Mutex
	// contains filtered or unexported fields
}

Hook is a hook designed for dealing with logs in test scenarios.

func New added in v0.0.21

func New() (*logrus.Logger, *Hook)

New sets up a test logger that produces no output. Use the returned hook to observe and make assertions about what was logged.

func NewNullLogger deprecated

func NewNullLogger() (*logrus.Logger, *Hook)

NewNullLogger Creates a discarding logger and installs the test hook.

Deprecated: Use New instead.

func (*Hook) CheckAllContained

func (t *Hook) CheckAllContained(tb testing.TB, strs ...string)

CheckAllContained looks through all the passed strings and verifies that all of them have been logged.

func (*Hook) CheckContained

func (t *Hook) CheckContained(tb testing.TB, strs ...string)

CheckContained looks through all the passed strings and verifies that at least one of those have been logged.

func (*Hook) CheckNotContained

func (t *Hook) CheckNotContained(tb testing.TB, strs ...string)

CheckNotContained looks through all the passed strings and verifies that none of those fragments have been logged.

func (*Hook) Entries

func (t *Hook) Entries() []*logrus.Entry

Entries is a thread safe accessor for all entries.

func (*Hook) Fire

func (t *Hook) Fire(e *logrus.Entry) error

Fire complies to the Hook interface.

func (*Hook) LastEntry

func (t *Hook) LastEntry() (l *logrus.Entry)

LastEntry returns the last entry that was logged or nil.

func (*Hook) Levels

func (t *Hook) Levels() []logrus.Level

Levels complies to the Hook interface.

func (*Hook) Reset

func (t *Hook) Reset()

Reset removes all Entries from this test hook.

func (*Hook) String

func (t *Hook) String() string

String returns the string representation of all the entries cumulatively logged in this Hook. If isolation is needed, prefer to make a new hook per test case.

Jump to

Keyboard shortcuts

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