zltest

package module
v0.11.3 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2021 License: BSD-2-Clause Imports: 9 Imported by: 0

README

Go Report Card GoDoc

Logging is an integral part of most applications and as such it has tobe tested. Package zltest provides facilities to test zerolog log messages.

In general zltest.Tester provides methods to test values (or existence) of specific fields in logged messages.

Installation

go get github.com/phpboyscout/zltest

Examples

func Test_ServiceLogsProperly(t *testing.T) {
    // --- Given ---
    // Crate zerolog test helper. 
    tst := zltest.New(t)

    // Configure zerolog and pas tester as a writer.     
    log := zerolog.New(tst).With().Timestamp().Logger()
    
    // Inject log to tested service or package.
    srv := MyService(log)

    // --- When ---
    srv.ExecuteSomeLogic()

    // --- Then ---

    // Test if log messages were generated properly.
    ent := tst.LastEntry()
    ent.ExpNum("key0", 123)
    ent.ExpMsg("message")
    ent.ExpLevel(zerolog.ErrorLevel)
}

License

BSD-2-Clause

Documentation

Overview

Package zltest provides facilities to test zerolog log messages.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Entries

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

Entries represents collection of zerolog log entries.

func (Entries) ExpBool

func (ets Entries) ExpBool(key string, exp bool)

ExpBool tests that at lest one entry has a field key, its value is boolean and equal to exp.

func (Entries) ExpDur

func (ets Entries) ExpDur(key string, exp time.Duration)

ExpDur tests that at least one log entry has a field key and its value is equal to exp time.Duration. The duration vale in the entry is multiplied by zerolog.DurationFieldUnit before the comparison.

func (Entries) ExpEntry

func (ets Entries) ExpEntry(n int) *Entry

ExpEntry returns nth logged entry.

func (Entries) ExpError

func (ets Entries) ExpError(exp string)

ExpError tests that at least one log entry error field (zerolog.ErrorFieldName) is equal to exp.

func (Entries) ExpLen

func (ets Entries) ExpLen(want int)

ExpLen tests that there is want number of entries.

func (Entries) ExpMsg

func (ets Entries) ExpMsg(exp string)

ExpMsg tests that at least one log entry message field (zerolog.MessageFieldName) is equal to exp.

func (Entries) ExpNum

func (ets Entries) ExpNum(key string, exp float64)

ExpNum tests that at least one log entry has a field key and its numerical value is equal to exp.

func (Entries) ExpStr

func (ets Entries) ExpStr(key string, exp string)

ExpStr tests that at least one log entry has a field key, its value is a string, and it's equal to exp.

func (Entries) ExpStrContains

func (ets Entries) ExpStrContains(key string, exp string)

ExpStrContains tests that at least one log entry has a field key, its value is a string, and it contains exp.

func (Entries) ExpTime

func (ets Entries) ExpTime(key string, exp time.Time)

ExpTime tests that at least one log entry has a field key, its value is a string representing time in zerolog.TimeFieldFormat and it's equal to exp.

func (Entries) Get

func (ets Entries) Get() []*Entry

Get returns the list of Entry in Entries

func (Entries) NotExpBool

func (ets Entries) NotExpBool(key string, exp bool)

NotExpBool tests that no log entry has a field key, its value is boolean and equal to exp.

func (Entries) NotExpDur

func (ets Entries) NotExpDur(key string, exp time.Duration)

NotExpDur tests that no log entry has a field key and its value is equal to exp time.Duration. The duration vale in the entry is multiplied by zerolog.DurationFieldUnit before the comparison.

func (Entries) NotExpError

func (ets Entries) NotExpError(exp string)

NotExpError tests that none of the log entry message fields (zerolog.ErrorFieldName) are equal to exp.

func (Entries) NotExpMsg

func (ets Entries) NotExpMsg(exp string)

NotExpMsg tests that none of the log entry message fields (zerolog.MessageFieldName) are equal to exp.

func (Entries) NotExpNum

func (ets Entries) NotExpNum(key string, exp float64)

NotExpNum tests that at least one log entry has a field key and its numerical value is equal to exp.

func (Entries) NotExpStr

func (ets Entries) NotExpStr(key string, exp string)

NotExpStr tests that no log entry has a field key, its value is a string, and it's equal to exp.

func (Entries) NotExpTime

func (ets Entries) NotExpTime(key string, exp time.Time)

NotExpTime tests that no one log entry has a field key, its value is a string representing time in zerolog.TimeFieldFormat and it's equal to exp.

func (Entries) Print

func (ets Entries) Print()

Print prints all zerolog log entries.

type Entry

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

Entry represents one zerolog log entry.

func (*Entry) Bool

func (ent *Entry) Bool(key string) (bool, KeyStatus)

Bool returns log entry field key as a boolean type.

func (*Entry) ExpBool

func (ent *Entry) ExpBool(key string, exp bool)

ExpBool tests log entry has a field key, its value is boolean and equal to exp.

func (*Entry) ExpDur

func (ent *Entry) ExpDur(key string, exp time.Duration)

ExpDur tests log entry has a field key and its value is equal to exp time.Duration. The duration vale in the entry is multiplied by zerolog.DurationFieldUnit before the comparison.

func (*Entry) ExpError

func (ent *Entry) ExpError(exp string)

ExpError tests log entry message field (zerolog.ErrorFieldName) is equal to exp.

func (*Entry) ExpKey

func (ent *Entry) ExpKey(key string)

ExpKey tests log entry has a field key.

func (*Entry) ExpLevel

func (ent *Entry) ExpLevel(exp zerolog.Level)

ExpLevel tests log entry level field (zerolog.LevelFieldName) is equal to exp.

func (*Entry) ExpLoggedWithin

func (ent *Entry) ExpLoggedWithin(exp time.Time, diff time.Duration)

ExpLoggedWithin tests log entry was logged at exp time. The actual time may be within +/- diff.

func (*Entry) ExpMsg

func (ent *Entry) ExpMsg(exp string)

ExpMsg tests log entry message field (zerolog.MessageFieldName) is equal to exp.

func (*Entry) ExpNum

func (ent *Entry) ExpNum(key string, exp float64)

ExpNum tests log entry has a field key and its numerical value is equal to exp.

func (*Entry) ExpStr

func (ent *Entry) ExpStr(key string, exp string)

ExpStr tests log entry has a field key, its value is a string, and it's equal to exp.

func (*Entry) ExpStrContains

func (ent *Entry) ExpStrContains(key string, exp string)

ExpStrContains tests log entry has a field key, its value is a string, and it contains exp.

func (*Entry) ExpStrs added in v0.11.2

func (ent *Entry) ExpStrs(key string, exp []string)

ExpStr tests log entry has a field key, its value is a string, and it's equal to exp.

func (*Entry) ExpTime

func (ent *Entry) ExpTime(key string, exp time.Time)

ExpTime tests log entry has a field key, its value is a string representing time in zerolog.TimeFieldFormat and it's equal to exp.

func (*Entry) ExpTimeWithin

func (ent *Entry) ExpTimeWithin(key string, exp time.Time, diff time.Duration)

ExpTimeWithin tests log entry has a field key, its value is a string representing time in zerolog.TimeFieldFormat and it's equal to exp time. The actual time may be within +/- diff.

func (*Entry) Float64

func (ent *Entry) Float64(key string) (float64, KeyStatus)

Float64 returns log entry field key as a float64 type.

func (*Entry) Map

func (ent *Entry) Map(key string) (map[string]interface{}, KeyStatus)

Map returns log entry key as a map.

func (*Entry) NotExpKey

func (ent *Entry) NotExpKey(key string)

NotExpKey tests log entry has no field key.

func (*Entry) Str

func (ent *Entry) Str(key string) (string, KeyStatus)

Str returns log entry field key as a string.

func (*Entry) String

func (ent *Entry) String() string

String implements fmt.Stringer interface and returns log entry as it was written to the writer.

func (*Entry) Strs

func (ent *Entry) Strs(key string) ([]string, KeyStatus)

Str returns log entry field key as a string.

func (*Entry) Time

func (ent *Entry) Time(key string) (time.Time, KeyStatus)

Time returns log entry field key as a time.Time. It uses zerolog.TimeFieldFormat to parse the time string representation.

type KeyStatus

type KeyStatus string

KeyStatus represents a status of searching and deserialization of a key in log entry.

const (
	// KeyFound is used when a field key is found successfully.
	KeyFound KeyStatus = "KeyFound"

	// KeyBadType is used when a field key is found, but it's not of expected type.
	KeyBadType KeyStatus = "KeyBadType"

	// KeyMissing is used when a field key is not in the log entry.
	KeyMissing KeyStatus = "KeyMissing"

	// KeyBadFormat is used when a field key is found but its format is wrong.
	KeyBadFormat KeyStatus = "KeyBadFormat"
)

type T

type T interface {
	// Error is equivalent to Log followed by Fail.
	Error(args ...interface{})

	// Errorf is equivalent to Logf followed by Fail.
	Errorf(format string, args ...interface{})

	// Fatal is equivalent to Log followed by FailNow.
	Fatal(args ...interface{})

	// Fatalf is equivalent to Logf followed by FailNow.
	Fatalf(format string, args ...interface{})

	// Helper marks the calling function as a test helper function.
	// When printing file and line information, that function will be skipped.
	// Helper may be called simultaneously from multiple goroutines.
	Helper()

	// Log formats its arguments using default formatting, analogous to Println,
	// and records the text in the error log. For tests, the text will be printed only if
	// the test fails or the -test.v flag is set. For benchmarks, the text is always
	// printed to avoid having performance depend on the value of the -test.v flag.
	Log(args ...interface{})

	// Logf formats its arguments according to the format, analogous to Printf, and
	// records the text in the error log. A final newline is added if not provided. For
	// tests, the text will be printed only if the test fails or the -test.v flag is
	// set. For benchmarks, the text is always printed to avoid having performance
	// depend on the value of the -test.v flag.
	Logf(format string, args ...interface{})

	// Cleanup registers a function to be called when the test (or subtest) and all its
	// subtests complete. Cleanup functions will be called in last added,
	// first called order.
	Cleanup(func())
}

T is a subset of testing.TB interface. It's primarily used to test zltest package but can be used to implement custom actions to be taken on errors.

type Tester

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

Tester represents zerolog log tester.

func New

func New(t T) *Tester

New creates new instance of zerolog tester.

func (*Tester) Entries

func (tst *Tester) Entries() Entries

Entries returns all logged entries in the order they were logged. It calls Fatal if any of the log entries cannot be decoded or Scanner error.

func (*Tester) Filter

func (tst *Tester) Filter(level zerolog.Level) Entries

Filter returns only entries matching log level.

func (*Tester) FirstEntry

func (tst *Tester) FirstEntry() *Entry

FirstEntry returns first log entry or nil if no log entries written to the Tester. It calls Fatal if any of the log entries cannot be decoded.

func (*Tester) LastEntry

func (tst *Tester) LastEntry() *Entry

LastEntry returns last log entry or nil if no log entries written to the Tester. It calls Fatal if any of the log entries cannot be decoded.

func (*Tester) Len

func (tst *Tester) Len() int

Len returns number of log messages written to the Tester.

func (*Tester) Logger

func (tst *Tester) Logger() zerolog.Logger

Logger returns zerolog.Logger using this tester as io.Writer.

func (*Tester) Reset

func (tst *Tester) Reset()

Reset resets the Tester.

func (*Tester) String

func (tst *Tester) String() string

String implements fmt.Stringer interface and returns everything written to the Tester so far. Calls Fatal on error.

func (*Tester) Write

func (tst *Tester) Write(p []byte) (n int, err error)

Write implements io.Writer interface.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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