Awesome
logrus-error
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/suzuki-shunsuke/logrus-error/logerr