logtest

package
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2024 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package logtest provides implementation of log.FieldLogger that allows writing tests for logging functionality. It was inspired by httptest (https://golang.org/pkg/net/http/httptest) from Go standard library.

Example
f := func(a, b int, logger log.FieldLogger) {
	logger.Info("calculation", log.Int("sum", a+b), log.String("hello", "world"))
}

logRecorder := NewRecorder()
f(40, 2, logRecorder)

// In real tests we can check that message with right fields were properly logged.

if logEntry, found := logRecorder.FindEntry("calculation"); found {
	fmt.Printf("[%s] %s\n", logEntry.Level, logEntry.Text)
	if logFieldSum, found := logEntry.FindField("sum"); found {
		fmt.Printf("sum: %d\n", logFieldSum.Int)
	}
	if logFieldHello, found := logEntry.FindField("hello"); found {
		fmt.Printf("hello: %s\n", logFieldHello.Bytes)
	}
}
Output:

[info] calculation
sum: 42
hello: world

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewLogger

func NewLogger() log.FieldLogger

NewLogger returns a new simple preconfigured logger (output: stderr, format: json, level: debug). It may be used in tests and should never be used in production due to slow performance.

func NewLoggerWithOpts

func NewLoggerWithOpts(opts LoggerOpts) log.FieldLogger

NewLoggerWithOpts returns logger instance configured according to options provided. If opts.Output value is nil it is set to os.Stderr.

Types

type LoggerOpts

type LoggerOpts struct {
	Output io.Writer
}

LoggerOpts allows to set custom options for test logger such as messages output target.

type RecordedEntry

type RecordedEntry struct {
	LoggerName string
	Fields     []log.Field
	Level      log.Level
	Time       time.Time
	Text       string
}

RecordedEntry represents recorded entry which was logged.

func (*RecordedEntry) FindField

func (re *RecordedEntry) FindField(key string) (*log.Field, bool)

FindField tries to find field in logging entry by key.

type Recorder

type Recorder struct {
	*log.LogfAdapter
	// contains filtered or unexported fields
}

Recorder is an implementation of log.FieldLogger that records all logged entries for later inspection in tests.

func NewRecorder

func NewRecorder() *Recorder

NewRecorder returns an initialized Recorder.

func (*Recorder) Entries

func (r *Recorder) Entries() []RecordedEntry

Entries returns all recorded logging entries.

func (*Recorder) FindAllEntriesByFilter

func (r *Recorder) FindAllEntriesByFilter(filter func(entry RecordedEntry) bool) []RecordedEntry

FindAllEntriesByFilter tries to find all recorded logging entries by filter (callback).

func (*Recorder) FindEntry

func (r *Recorder) FindEntry(msg string) (RecordedEntry, bool)

FindEntry tries to find recorded logging entry by message.

func (*Recorder) FindEntryByFilter

func (r *Recorder) FindEntryByFilter(filter func(entry RecordedEntry) bool) (RecordedEntry, bool)

FindEntryByFilter tries to find recorded logging entry by filter (callback).

func (*Recorder) Reset

func (r *Recorder) Reset()

Reset resets all recorded logs.

func (*Recorder) With

func (r *Recorder) With(fs ...log.Field) log.FieldLogger

With returns a new Recorder with the given additional fields.

func (*Recorder) WithLevel

func (r *Recorder) WithLevel(level log.Level) log.FieldLogger

WithLevel returns a new Recorder with the given additional level check. All log messages below ("debug" is a minimal level, "error" - maximal) the given AND previously set level will be ignored (i.e. it makes sense to only increase level).

Jump to

Keyboard shortcuts

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