log

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 5, 2019 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package log contains the Logger interface and various helper functions for logging.

After they instantiated an appropriate logger adapter, it should be enough for most applications to rely on the types and functions defined in package log exclusively.

Some applications may have very simple logging needs. For those cases package log provides a very basic writer logger.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenerateKEYVALs added in v0.2.0

func GenerateKEYVALs(tb testing.TB, n int) []interface{}

GenerateKEYVALs generates a fixed amount of key value pairs.

func JSONFormatter

func JSONFormatter(kvs []interface{}) ([]byte, error)

JSONFormatter marshals the passed key-value pairs into a JSON object.

func Log

func Log(logger Logger, kvs ...interface{})

Log calls logger.Log with the passed kvs, if logger is not nil.

Should logger.Log return an error, Log discards it.

func PlainTextFormatter

func PlainTextFormatter(kvs []interface{}) ([]byte, error)

PlainTextFormatter converts the passed kvs to strings and joins them separated by a coma and a space.

func StressTestLogger added in v0.2.0

func StressTestLogger(tb testing.TB, factory func() Logger, nGoRoutines, nMSGs int)

StressTestLogger exercises the logger returned by factory using multiple Go routines.

Each Go routine writes nMSGs before it terminates.

Types

type Formatter

type Formatter func([]interface{}) ([]byte, error)

Formatter converts the passed slice of interface{} to a byte slice.

A logger can then use the byte slice to write it to the log. A Formatter may return an error to indicate it could not write the slice to the log.

type Logger

type Logger interface {
	Log(kvs ...interface{}) error
}

Logger is the fundamental interface for all log operations.

Logger is the same interface as GoKit's Logger interface (https://godoc.org/github.com/go-kit/kit/log#Logger) and follows the same contract: Log creates a log event from kvs, a variadic sequence of alternating keys and values. Implementations must be safe for concurrent use by multiple goroutines. In particular, any implementation of Logger that appends to kvs or modifies or retains any of its elements must make a copy first.

In addition to the basic contract specified by GoKit implementations of Logger MAY extend it with the following additional rules.

Implementations MAY look for the keywords "level" or "lvl" and assume that the value of those keywords defines a logging level. Implementations must always support both keywords, "level" and "lvl". Additionally they must expect callers to use both keywords interchangeably. If kvs contains "level" and "lvl" at the same time they must give preference to "level". Should kvs contain neither "level" nor "lvl" they must assume an appropriate default level. Implementations MAY skip creating an log entry based on the level or the assumed default level.

Furthermore implementations MAY look for the keywords "message" or "msg" and assume that the value of those keywords defines a log message which may be treated specially. Implementations must always support both keywords, "message" and "msg". Additionally they must expect callers to use both keywords interchangeably. If kvs contains "message" and "msg" at the same time they must give preference to "message". Should kvs contain neither "message" nor "msg" they must assume an appropriate default message.

func NewNOPLogger

func NewNOPLogger() Logger

NewNOPLogger creates a logger that just discards its entries.

func NewStdlib

func NewStdlib(l *stdliblog.Logger, f Formatter) Logger

NewStdlib instantiates golf's standard library logger adapter using the passed logger. Log entries are formatted using the passed formatter. If f is nil log.PlainTextFormatter is used.

func With

func With(logger Logger, kvs ...interface{}) Logger

With creates a contextual-logger, i.e. a logger that will always add the kvs passed to With to the final log entry.

It is safe to pass nil for the logger. In this case nil will be returned.

type TestLogEntry added in v0.2.0

type TestLogEntry map[interface{}]interface{}

TestLogEntry represents a single log entry stored by the TestLogger.

func NewTestLogEntry added in v0.2.0

func NewTestLogEntry(kvs ...interface{}) (TestLogEntry, error)

NewTestLogEntry creates a new TestLogEntry from the passed kvs. The number of kvs must be even, otherwise an error is returned.

type TestLogger added in v0.2.0

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

TestLogger stores the entries internally without formatting them. TestLogger is save for concurrent use.

func (*TestLogger) AssertHasMatchingLogEntries added in v0.2.0

func (l *TestLogger) AssertHasMatchingLogEntries(t *testing.T, expected int, pred func(TestLogEntry) bool) int

AssertHasMatchingLogEntries searches the TestLogger's log entries for any that match pred. It adds a test error if expected does not match the number of matching entries. AssertHasMatchingLogEntries returns the number of matching entries.

func (*TestLogger) CountMatchingLogEntries added in v0.2.0

func (l *TestLogger) CountMatchingLogEntries(pred func(TestLogEntry) bool) int

CountMatchingLogEntries returns the number of log entries matching pred.

func (*TestLogger) Log added in v0.2.0

func (l *TestLogger) Log(kvs ...interface{}) error

Log adds kvs as an entry to the TestLoggers internal structure of log entries.

type Wither

type Wither interface {
	With(...interface{}) Logger
}

Wither wraps the With method which is used by the implementing type to concatenate the passed key-value pairs and its internal store of key-value pairs. The implementor then returns a new version of itself containing the concatenated key value pairs.

Implementors must make sure, that the wither is safe for concurrent use by multiple go routines. Especially they must make sure to copy their internal version of key-value pairs as well as the passed key-value pairs.

Jump to

Keyboard shortcuts

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