## Search

Page index: https://gasmod.dev/llms.txt. Add index.md to any page URL for its Markdown.

This site has a search endpoint: https://context7.com/api/v2/ask?siteKey=ask_b845b566cb63bd033e9c576d&query=<URL-encoded question>. It returns documentation that answers the question, with a source link for each part. No API key is needed. If nothing matches, it says so.

# log/logtest

> Package logtest provides RecordingLogger, a gas.Logger for tests that keeps every event it sends, with its level, message and fields.

The Markdown of https://gasmod.dev/reference/log/logtest/ from the Gas documentation, whose index for LLMs is https://gasmod.dev/llms.txt

```go
import "github.com/gasmod/gas/log/logtest"
```

Package logtest provides [RecordingLogger](#RecordingLogger), a gas.Logger for tests that
keeps every event it sends, with its level, message and fields.

## Usage

```go
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

An [Entry](#Entry) holds:

- the level, one of `trace`, `debug`, `info`, `warn` and `error`;
- 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

A RecordingLogger is safe for concurrent use; events and the contexts
from With are not.


## API reference

<a id="Entry"></a>

### type Entry

```go
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.

<a id="RecordingLogger"></a>

### type RecordingLogger

```go
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.

<a id="New"></a>

#### func New

```go
func New() *RecordingLogger
```

New returns an empty RecordingLogger.

<a id="RecordingLogger.Debug"></a>

#### func (*RecordingLogger) Debug

```go
func (l *RecordingLogger) Debug(msg string) gas.LogEvent
```

Debug starts an event at level `debug`.

<a id="RecordingLogger.Entries"></a>

#### func (*RecordingLogger) Entries

```go
func (l *RecordingLogger) Entries() []Entry
```

Entries 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.

<a id="RecordingLogger.Error"></a>

#### func (*RecordingLogger) Error

```go
func (l *RecordingLogger) Error(msg string) gas.LogEvent
```

Error starts an event at level `error`.

<a id="RecordingLogger.Flush"></a>

#### func (*RecordingLogger) Flush

```go
func (*RecordingLogger) Flush()
```

Flush does nothing.

<a id="RecordingLogger.Info"></a>

#### func (*RecordingLogger) Info

```go
func (l *RecordingLogger) Info(msg string) gas.LogEvent
```

Info starts an event at level `info`.

<a id="RecordingLogger.Trace"></a>

#### func (*RecordingLogger) Trace

```go
func (l *RecordingLogger) Trace(msg string) gas.LogEvent
```

Trace starts an event at level `trace`.

<a id="RecordingLogger.Warn"></a>

#### func (*RecordingLogger) Warn

```go
func (l *RecordingLogger) Warn(msg string) gas.LogEvent
```

Warn starts an event at level `warn`.

<a id="RecordingLogger.With"></a>

#### func (*RecordingLogger) With

```go
func (l *RecordingLogger) With() gas.LoggerContext
```

With 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.
