https://github.com/suzuki-shunsuke/logrus-error
Embed logrus.Fields into error
https://github.com/suzuki-shunsuke/logrus-error
library logging
Last synced: about 2 months ago
JSON representation
Embed logrus.Fields into error
- Host: GitHub
- URL: https://github.com/suzuki-shunsuke/logrus-error
- Owner: suzuki-shunsuke
- License: mit
- Created: 2021-08-30T07:43:51.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2025-02-11T18:49:08.000Z (3 months ago)
- Last Synced: 2025-02-27T04:25:32.662Z (2 months ago)
- Topics: library, logging
- Language: Go
- Homepage:
- Size: 82 KB
- Stars: 3
- Watchers: 3
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# logrus-error
[](https://pkg.go.dev/github.com/suzuki-shunsuke/logrus-error)
[](https://github.com/suzuki-shunsuke/logrus-error/actions)
[](https://github.com/suzuki-shunsuke/logrus-error)
[](https://raw.githubusercontent.com/suzuki-shunsuke/logrus-error/main/LICENSE)Go small library to embed [logrus](https://github.com/sirupsen/logrus).Fields into error.
## Motivation
With [fmt.Errorf](https://pkg.go.dev/fmt#Errorf), you can add additional context to error.
e.g.
```go
fmt.Errorf("get a user: %w", err)
```[logrus](https://github.com/sirupsen/logrus) is one of most popular structured logging library.
e.g.
```go
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](https://pkg.go.dev/github.com/suzuki-shunsuke/logrus-error/logerr#WithFields): Add structured data to error
* [WithError](https://pkg.go.dev/github.com/suzuki-shunsuke/logrus-error/logerr#WithError): Get structured data from error and return logrus.EntryAS IS (without logrus-error)
```go
return fmt.Errorf("get a user (username: %s): %w", username, err)
``````go
logrus.WithError(err).Error("add a member to a group")
```TO BE (with logrus-error)
```go
return logerr.WithFields(fmt.Errorf("get a user: %w", err), logrus.Fields{
"username": username,
})
``````go
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](https://pkg.go.dev/fmt#hdr-Printing).
## Document
Please see https://pkg.go.dev/github.com/suzuki-shunsuke/logrus-error/logerr
## License
[MIT](LICENSE)