zerolog-error

Embed fields into error for zerolog

MIT License

Stars
2

zerolog-error

Embed fields into error for zerolog.

Go small library to embed fields into error for zerolog.

Similar Projects

Motivation

Embed structured data into error and output it with zerolog.

fmt.Errorf enables to add additional context to error.

e.g.

fmt.Errorf("get a user: %w", err)

rs/zerolog is one of most popular structured logging library.

e.g.

logger.Error().Err(err).Str("username", username).Msg("get a user")

fmt.Errorf is very useful, but you can add only a string to error as context. You can't add structured data to error. If you use zerolog, you may want to add structured data to error.

This library provides small APIs to embed structured data into error and output it with zerolog.

How to use

import (
	"github.com/suzuki-shunsuke/zerolog-error/zerr"
)

There are two APIs.

  • WithFields(err error, fields ...Field) error: Embed fields into error
  • WithError(ev *zerolog.Event, err error) *zerolog.Event: Get fields from error and add them to an event

e.g.

func updateUser() error {
	// ...
	return zerr.WithFields(errors.New("get a user"), zerr.Str("id", "foo"))
}
	if err := updateUser(); err != nil {
		zerr.WithError(log.Error(), err).Send("update a user")
	}

Document

Please see https://pkg.go.dev/github.com/suzuki-shunsuke/zerolog-error/zerr

LICENSE

MIT

Package Rankings
Top 13.79% on Proxy.golang.org
Badges
Extracted from project README
Go Reference