https://github.com/suzuki-shunsuke/slog-error
Embed Attrs into error for slog
https://github.com/suzuki-shunsuke/slog-error
golang library logging oss
Last synced: 4 months ago
JSON representation
Embed Attrs into error for slog
- Host: GitHub
- URL: https://github.com/suzuki-shunsuke/slog-error
- Owner: suzuki-shunsuke
- License: mit
- Created: 2023-12-29T12:51:40.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2025-02-20T04:37:03.000Z (over 1 year ago)
- Last Synced: 2025-02-20T05:28:58.147Z (over 1 year ago)
- Topics: golang, library, logging, oss
- Language: Go
- Homepage:
- Size: 11.7 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# slog-error
[](https://deepwiki.com/suzuki-shunsuke/slog-error)
[](https://pkg.go.dev/github.com/suzuki-shunsuke/slog-error)
Go library to embed args into error for [slog](https://pkg.go.dev/log/slog)
## Usage
This library provides only two APIs.
```go
// WithError gets args from err and returns a new logger with err and args.
func WithError(logger *slog.Logger, err error) *slog.Logger
// With returns an error with args.
func With(err error, args ...any) error
```
```go
package main
import (
"errors"
"fmt"
"log/slog"
"os"
"github.com/suzuki-shunsuke/slog-error/slogerr"
)
func main() {
logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{}))
if err := core(); err != nil {
// time=2023-12-29T22:30:06.992+09:00 level=ERROR msg="command failed" name=mike error="user is not found"
slogerr.WithError(logger, err).Error("command failed")
}
}
func core() error {
if err := getUser(); err != nil {
return fmt.Errorf("get a user: %w", err)
}
return nil
}
func getUser() error {
return slogerr.With(
errors.New("user is not found"),
"name", "mike")
}
```
## LICENSE
[MIT](LICENSE)