Skip to content
Gasv0.10.0

gas/log

log/zerolog

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.

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

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.

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.

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

ErrNilLogger, which is log.ErrNilLogger, to be tested with errors.Is: WithLogger was given nil. The error, as returned:

gas/log: logger is nil

var ErrNilLogger = log.ErrNilLogger

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

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.

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.

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

Debug starts an event at zerolog.DebugLevel.

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

Error starts an event at zerolog.ErrorLevel.

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.

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

Info starts an event at zerolog.InfoLevel.

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

Trace starts an event at zerolog.TraceLevel.

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

Warn starts an event at zerolog.WarnLevel.

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.

type Option func(*Logger)

Option configures a Logger.

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.