logrus-error

module
v0.0.0-...-bdf73fd Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 18, 2023 License: MIT

README

logrus-error

Go Reference Build Status GitHub last commit License

Go small library to embed logrus.Fields into error.

Motivation

With fmt.Errorf, you can add additional context to error.

e.g.

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

logrus is one of most popular structured logging library.

e.g.

logrus.WithFields(logrus.Fields{
	"username": username,
}).WithError(err).Error("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 logrus, you may want to add structured data to error.

logrus-error is a small library to add structured data to error and get structured data from error for logging.

Mainly logrus-error provides only two simple API.

  • WithFields: Add structured data to error
  • WithError: Get structured data from error and return logrus.Entry

AS IS (without logrus-error)

return fmt.Errorf("get a user (username: %s): %w", username, err)
logrus.WithError(err).Error("add a member to a group")

TO BE (with logrus-error)

return logerr.WithFields(fmt.Errorf("get a user: %w", err), logrus.Fields{
	"username": username,
})
entry := logrus.NewEntry(logrus.New())
logerr.WithError(entry, err).Error("add a member to a group")

Using logrus-error, you can add structured data to error as context. You don't have to construct a string with fmt's format.

Document

Please see https://pkg.go.dev/github.com/sulaiman-coder/logrus-error/logerr

License

MIT

Directories

Path Synopsis
Package logerr provides small API to embed logrus.Fields into error.
Package logerr provides small API to embed logrus.Fields into error.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL