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].
Levels
Section titled “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
Section titled “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
Section titled “Concurrency”A Logger is safe for concurrent use, as far as its writer is; events and the contexts from With are not.
Errors
Section titled “Errors”ErrNilLogger, which is log.ErrNilLogger, to be tested with errors.Is: WithLogger was given nil. The error, as returned:
gas/log: logger is nilAPI reference
Section titled “API reference”Variables
Section titled “Variables”var ErrNilLogger = log.ErrNilLoggerErrNilLogger indicates that WithLogger was given a nil logger. It is log.ErrNilLogger, so errors.Is matches either.
func New
Section titled “func New”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
Section titled “type Logger”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 (*Logger) Debug
Section titled “func (*Logger) Debug”func (l *Logger) Debug(msg string) gas.LogEventDebug starts an event at zerolog.DebugLevel.
func (*Logger) Error
Section titled “func (*Logger) Error”func (l *Logger) Error(msg string) gas.LogEventError starts an event at zerolog.ErrorLevel.
func (*Logger) Flush
Section titled “func (*Logger) Flush”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 (*Logger) Info
Section titled “func (*Logger) Info”func (l *Logger) Info(msg string) gas.LogEventInfo starts an event at zerolog.InfoLevel.
func (*Logger) Trace
Section titled “func (*Logger) Trace”func (l *Logger) Trace(msg string) gas.LogEventTrace starts an event at zerolog.TraceLevel.
func (*Logger) Warn
Section titled “func (*Logger) Warn”func (l *Logger) Warn(msg string) gas.LogEventWarn starts an event at zerolog.WarnLevel.
func (*Logger) With
Section titled “func (*Logger) With”func (l *Logger) With() gas.LoggerContextWith returns a context for a logger that writes to the same writer with the fields added to the context.
type Option
Section titled “type Option”type Option func(*Logger)Option configures a Logger.
func WithLogger
Section titled “func WithLogger”func WithLogger(logger *rz.Logger) OptionWithLogger 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.