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 ¶
- func NewLogger() log.FieldLogger
- func NewLoggerWithOpts(opts LoggerOpts) log.FieldLogger
- type LoggerOpts
- type RecordedEntry
- type Recorder
- func (r *Recorder) Entries() []RecordedEntry
- func (r *Recorder) FindAllEntriesByFilter(filter func(entry RecordedEntry) bool) []RecordedEntry
- func (r *Recorder) FindEntry(msg string) (RecordedEntry, bool)
- func (r *Recorder) FindEntryByFilter(filter func(entry RecordedEntry) bool) (RecordedEntry, bool)
- func (r *Recorder) Reset()
- func (r *Recorder) With(fs ...log.Field) log.FieldLogger
- func (r *Recorder) WithLevel(level log.Level) log.FieldLogger
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 ¶
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.
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 (*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) 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).