## 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/zerolog

> Package zerolog provides a gas.Logger over a zerolog.Logger.

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

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

Package zerolog provides a gas.Logger over a zerolog.Logger.

It is a package of its own so that only an app that uses it depends on
zerolog. The events follow the contract of package
github.com/gasmod/gas/log.

## Usage

```go
zl := zerolog.New(os.Stdout).With().Timestamp().Logger()

logger, err := gaszerolog.New(gaszerolog.WithLogger(&zl))()
if err != nil {
	return err
}

logger.Info("order placed").Str("order", "A-1").Int("items", 3).Send()
```

Here zerolog is github.com/rs/zerolog and gaszerolog this package. The
line written is the JSON object with `level` `info`, `order`, `items`,
the time and `message` `order placed`.

In an app, register the constructor with
gas.WithSingletonService\[gas.Logger\].

## Levels

Trace, Debug, Info, Warn and Error are zerolog's TraceLevel, DebugLevel,
InfoLevel, WarnLevel and ErrorLevel. An event at a level the logger does
not enable is a shared no-op event, so its fields cost nothing.

## Events

Each field is encoded when its method is called, on the caller's
goroutine.

An event is sent at most once. Send drops the zerolog event, which
zerolog then reuses for another. So every method called after Send, Send
included, does nothing and cannot reach that other event.

With zerolog's Caller on the logger, the caller written is the line that
called Send.

The encodings are zerolog's:

- Err with a nil error leaves the field out, and Err otherwise writes
the Error text.
- Any with an error does the same as Err. Any with another value
writes zerolog's Interface encoding, its JSON.
- Duration writes zerolog's Dur, in zerolog.DurationFieldUnit,
milliseconds unless changed.

A logger from With carries the fields added to its context, before each
event's own; its parent is unchanged. Calling Logger again on the same
context, after adding more fields, gives a second logger with them all;
the first is unchanged.

Flush does nothing: zerolog writes each event on Send. A writer that
buffers, such as a diode writer, is flushed by its owner.

## Concurrency

A Logger is safe for concurrent use, as far as its writer is; events
and the contexts from With are not.

## Errors

[ErrNilLogger](#ErrNilLogger), which is log.ErrNilLogger, to be tested with
[errors.Is](https://pkg.go.dev/errors#Is): WithLogger was given nil. The error, as returned:

```go
gas/log: logger is nil
```


## API reference

### Variables

<a id="ErrNilLogger"></a>

```go
var ErrNilLogger = log.ErrNilLogger
```

ErrNilLogger indicates that WithLogger was given a nil logger. It is
log.ErrNilLogger, so errors.Is matches either.

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

#### func New

```go
func New(opts ...Option) func() (*Logger, error)
```

New returns the constructor of a Logger with opts applied, in the shape
the gas container expects. The constructor returns an error wrapping
ErrNilLogger when WithLogger was given nil.

<a id="Logger"></a>

### type Logger

```go
type Logger struct {
	// contains filtered or unexported fields
}
```

Logger is a gas.Logger over a \*zerolog.Logger, as described in the
package documentation. It is safe for concurrent use; its events are
not.

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

#### func (*Logger) Debug

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

Debug starts an event at zerolog.DebugLevel.

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

#### func (*Logger) Error

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

Error starts an event at zerolog.ErrorLevel.

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

#### func (*Logger) Flush

```go
func (*Logger) Flush()
```

Flush does nothing: zerolog writes each event on Send. A writer that
buffers, such as a diode, is flushed by its owner.

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

#### func (*Logger) Info

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

Info starts an event at zerolog.InfoLevel.

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

#### func (*Logger) Trace

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

Trace starts an event at zerolog.TraceLevel.

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

#### func (*Logger) Warn

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

Warn starts an event at zerolog.WarnLevel.

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

#### func (*Logger) With

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

With returns a context for a logger that writes to the same writer with
the fields added to the context.

<a id="Option"></a>

### type Option

```go
type Option func(*Logger)
```

Option configures a Logger.

<a id="WithLogger"></a>

#### func WithLogger

```go
func WithLogger(logger *rz.Logger) Option
```

WithLogger sets the \*zerolog.Logger the Logger writes to. A nil logger
fails the constructor with ErrNilLogger.

Default: zerolog's global logger, github.com/rs/zerolog/log.Logger, read
through a pointer, so a later assignment to it is seen.
