gas/log
log/logtest
import "github.com/gasmod/gas/log/logtest"Package logtest provides RecordingLogger, a gas.Logger for tests that keeps every event it sends, with its level, message and fields.
logger := logtest.New()
svc.Run(logger)
for _, e := range logger.Entries() { if e.Level == "error" { t.Errorf("unexpected error: %s %v", e.Msg, e.Fields) }}Entries returns the events sent so far, in order. The zero value of a RecordingLogger is ready to use, as New’s is.
Entries
Section titled “Entries”An Entry holds:
- the level, one of
trace,debug,info,warnanderror; - the message;
- the fields by key, the values as they were given. A field given twice keeps the last value.
Entries returns a copy, with copies of the field maps, so changing it changes no later result.
An event is recorded by Send, and at most once: every method called after Send, Send included, does nothing.
A logger from With records into the same entries as the logger it came from, and puts the fields of its context on every event before the event’s own, so an event’s field of the same key wins. Calling Logger again on the same context, after adding more fields, gives a second logger with them all; the first is unchanged. Entries of any of these loggers returns them all.
Flush does nothing.
Concurrency
Section titled “Concurrency”A RecordingLogger is safe for concurrent use; events and the contexts from With are not.
API reference
Section titled “API reference”type Entry
Section titled “type Entry”type Entry struct { // Fields holds the event's fields by key, the logger's own included. Fields map[string]any // Level is `trace`, `debug`, `info`, `warn` or `error`. Level string // Msg is the event's message. Msg string}Entry is one event a RecordingLogger received.
type RecordingLogger
Section titled “type RecordingLogger”type RecordingLogger struct { // contains filtered or unexported fields}RecordingLogger is a gas.Logger that keeps every event it sends, as described in the package documentation. The zero value is ready to use. It is safe for concurrent use; its events are not.
func New
Section titled “func New”func New() *RecordingLoggerNew returns an empty RecordingLogger.
func (*RecordingLogger) Debug
Section titled “func (*RecordingLogger) Debug”func (l *RecordingLogger) Debug(msg string) gas.LogEventDebug starts an event at level debug.
func (*RecordingLogger) Entries
Section titled “func (*RecordingLogger) Entries”func (l *RecordingLogger) Entries() []EntryEntries returns a copy of the events sent so far, in order, by l and every logger that shares its entries. The copy holds copies of the field maps.
func (*RecordingLogger) Error
Section titled “func (*RecordingLogger) Error”func (l *RecordingLogger) Error(msg string) gas.LogEventError starts an event at level error.
func (*RecordingLogger) Flush
Section titled “func (*RecordingLogger) Flush”func (*RecordingLogger) Flush()Flush does nothing.
func (*RecordingLogger) Info
Section titled “func (*RecordingLogger) Info”func (l *RecordingLogger) Info(msg string) gas.LogEventInfo starts an event at level info.
func (*RecordingLogger) Trace
Section titled “func (*RecordingLogger) Trace”func (l *RecordingLogger) Trace(msg string) gas.LogEventTrace starts an event at level trace.
func (*RecordingLogger) Warn
Section titled “func (*RecordingLogger) Warn”func (l *RecordingLogger) Warn(msg string) gas.LogEventWarn starts an event at level warn.
func (*RecordingLogger) With
Section titled “func (*RecordingLogger) With”func (l *RecordingLogger) With() gas.LoggerContextWith returns a context for a logger that records into l’s entries, with the fields of l and those added to the context on every event.