Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ichizero/errlog
errlog is a slog handler that logs errors with stack traces easily.
https://github.com/ichizero/errlog
go golang log slog slog-handler
Last synced: about 2 months ago
JSON representation
errlog is a slog handler that logs errors with stack traces easily.
- Host: GitHub
- URL: https://github.com/ichizero/errlog
- Owner: ichizero
- License: apache-2.0
- Created: 2024-03-02T14:07:12.000Z (10 months ago)
- Default Branch: main
- Last Pushed: 2024-05-09T11:54:06.000Z (8 months ago)
- Last Synced: 2024-06-21T14:23:17.104Z (7 months ago)
- Topics: go, golang, log, slog, slog-handler
- Language: Go
- Homepage:
- Size: 17.6 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# errlog
[![Test](https://github.com/ichizero/errlog/actions/workflows/test.yml/badge.svg)](https://github.com/ichizero/errlog/actions/workflows/test.yml)
[![Go Reference](https://pkg.go.dev/badge/github.com/ichizero/errlog.svg)](https://pkg.go.dev/github.com/ichizero/errlog)
[![Codecov](https://codecov.io/gh/ichizero/errlog/branch/main/graph/badge.svg)](https://codecov.io/gh/ichizero/errlog)
[![Go Report Card](https://goreportcard.com/badge/github.com/ichizero/errlog)](https://goreportcard.com/report/github.com/ichizero/errlog)`errlog` is a error logging package based on [log/slog](https://pkg.go.dev/log/slog) standard library.
It provides error logging with stack trace and source location.
It does not require any third-party package.## 🚀 Installation
```bash
go get github.com/ichizero/errlog
```## 🧐 Usage
### Initialize logger
`errlog.NewHandler` wraps `slog.Handler`, so you can provide `*slog.JSONHandler`, `*slog.TextHandler`,
or any other handler.```go
h := slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{AddSource: true})
hErr := errlog.NewHandler(h, &errlog.HandlerOptions{OverrideSource: true, SuppressStackTrace: false})
slog.SetDefault(slog.New(hErr))
```### Logging error with stack trace
#### With errlog.Err
`errlog.Err` wraps error with stack trace and returns `slog.Attr` with key `error`.```go
err := errors.New("test error")
slog.ErrorContext(ctx, "test", errlog.Err(err))
```#### With custom error
`errlog.NewHandler` outputs stack trace with the error that implements `errlog.StackTrace` interface,
so you can provide custom error with stack trace.```go
type yourCustomError struct {
err error
stack []uintptr
}func (e yourCustomError) Stack() []uintptr {
return e.stack
}
```If so, you can log stack trace without using `errlog.Err`.
```go
err := newYourCustomError("error")
slog.ErrorContext(ctx, "test", slog.Any("error", err))
```#### Example usage
```go
package mainimport (
"context"
"errors"
"log/slog"
"os""github.com/ichizero/errlog"
)func main() {
h := slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{AddSource: true})
hErr := errlog.NewHandler(h, &errlog.HandlerOptions{OverrideSource: true, SuppressStackTrace: false})
slog.SetDefault(slog.New(hErr))ctx := context.Background()
err := errors.New("test error")
slog.ErrorContext(ctx, "test", errlog.Err(err))err = errlog.WrapError(err)
slog.ErrorContext(ctx, "test", slog.Any("error", err))
}
```