v1.27.0 Latest Latest

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

Go to latest
Published: Feb 20, 2024 License: MIT Imports: 5 Imported by: 235



Package observer provides a zapcore.Core that keeps an in-memory, encoding-agnostic representation of log entries. It's useful for applications that want to unit test their log output without tying their tests to a particular output encoding.



This section is empty.


This section is empty.


This section is empty.


type LoggedEntry

type LoggedEntry struct {
	Context []zapcore.Field

An LoggedEntry is an encoding-agnostic representation of a log message. Field availability is context dependant.

func (LoggedEntry) ContextMap added in v1.6.0

func (e LoggedEntry) ContextMap() map[string]interface{}

ContextMap returns a map for all fields in Context.

type ObservedLogs

type ObservedLogs struct {
	// contains filtered or unexported fields

ObservedLogs is a concurrency-safe, ordered collection of observed logs.

func New

New creates a new Core that buffers logs in memory (without any encoding). It's particularly useful in tests.

func (*ObservedLogs) All

func (o *ObservedLogs) All() []LoggedEntry

All returns a copy of all the observed logs.

func (*ObservedLogs) AllUntimed

func (o *ObservedLogs) AllUntimed() []LoggedEntry

AllUntimed returns a copy of all the observed logs, but overwrites the observed timestamps with time.Time's zero value. This is useful when making assertions in tests.

func (*ObservedLogs) Filter added in v1.18.0

func (o *ObservedLogs) Filter(keep func(LoggedEntry) bool) *ObservedLogs

Filter returns a copy of this ObservedLogs containing only those entries for which the provided function returns true.

func (*ObservedLogs) FilterField added in v1.1.0

func (o *ObservedLogs) FilterField(field zapcore.Field) *ObservedLogs

FilterField filters entries to those that have the specified field.

func (*ObservedLogs) FilterFieldKey added in v1.17.0

func (o *ObservedLogs) FilterFieldKey(key string) *ObservedLogs

FilterFieldKey filters entries to those that have the specified key.

func (*ObservedLogs) FilterLevelExact added in v1.18.0

func (o *ObservedLogs) FilterLevelExact(level zapcore.Level) *ObservedLogs

FilterLevelExact filters entries to those logged at exactly the given level.

func (*ObservedLogs) FilterMessage added in v1.1.0

func (o *ObservedLogs) FilterMessage(msg string) *ObservedLogs

FilterMessage filters entries to those that have the specified message.

func (*ObservedLogs) FilterMessageSnippet added in v1.3.0

func (o *ObservedLogs) FilterMessageSnippet(snippet string) *ObservedLogs

FilterMessageSnippet filters entries to those that have a message containing the specified snippet.

func (*ObservedLogs) Len

func (o *ObservedLogs) Len() int

Len returns the number of items in the collection.

func (*ObservedLogs) TakeAll

func (o *ObservedLogs) TakeAll() []LoggedEntry

TakeAll returns a copy of all the observed logs, and truncates the observed slice.

Jump to

Keyboard shortcuts

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