zltest

package module
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: Jan 27, 2022 License: BSD-2-Clause Imports: 9 Imported by: 4

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/rzajac/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 added in v0.3.0

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

Entries represents collection of zerolog log entries.

func (Entries) ExpBool added in v0.3.0

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 added in v0.3.0

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 added in v0.6.0

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

ExpEntry returns nth logged entry.

func (Entries) ExpErr added in v0.11.0

func (ets Entries) ExpErr(exp error)

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

func (Entries) ExpError added in v0.9.0

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 added in v0.6.0

func (ets Entries) ExpLen(want int)

ExpLen tests that there is want number of entries.

func (Entries) ExpMsg added in v0.3.0

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 added in v0.3.0

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 added in v0.3.0

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 added in v0.8.0

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 added in v0.3.0

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 added in v0.3.0

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

Get returns the list of Entry in Entries

func (Entries) NotExpBool added in v0.3.0

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 added in v0.3.0

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) NotExpErr added in v0.11.0

func (ets Entries) NotExpErr(exp error)

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

func (Entries) NotExpError added in v0.9.0

func (ets Entries) NotExpError(exp string)

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

func (Entries) NotExpMsg added in v0.3.0

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 added in v0.3.0

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 added in v0.3.0

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 added in v0.3.0

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 added in v0.8.0

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) ExpErr added in v0.11.0

func (ent *Entry) ExpErr(exp error)

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

func (*Entry) ExpError added in v0.9.0

func (ent *Entry) ExpError(exp string)

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

func (*Entry) ExpKey added in v0.7.0

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) ExpNumKeys added in v0.12.0

func (ent *Entry) ExpNumKeys(n int)

ExpNumKeys tests log entry has n keys.

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 added in v0.8.0

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) 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 added in v0.8.0

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

Map returns log entry key as a map.

func (*Entry) NotExpKey added in v0.7.0

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) 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 added in v0.5.0

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

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

func (*Tester) Reset added in v0.4.0

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