cockroachdb / errors

Go error library with error portability over the network
Apache License 2.0
2.04k stars 66 forks source link

Implement slog.LogValuer #129

Open nozo-moto opened 10 months ago

nozo-moto commented 10 months ago

https://github.com/cockroachdb/errors/issues/107

This change is Reviewable

code ``` go package main import ( "context" "log/slog" "os" "github.com/cockroachdb/errors" ) func f1() error { return errors.Wrap(f2(), "wrapped error 1") } func f2() error { return errors.Wrap(f3(), "wrapped error 2") } func f3() error { return errors.Wrap(f4(), "wrapped error 3") } func f4() error { return errors.Wrap(f5(), "wrapped error 4") } func f5() error { return errors.New("error root") } func main() { ctx := context.Background() err := f1() logger := slog.New(slog.NewJSONHandler(os.Stdout, nil)) logger.InfoContext(ctx, "test", slog.Any("error", err)) } ```
result ``` json { "time": "2023-09-18T17:57:09.438517+09:00", "level": "INFO", "msg": "test", "error": { "message": "wrapped error 1: wrapped error 2: wrapped error 3: wrapped error 4: error root", "stacktrace": { "main.f1": { "file": "/Users/nozo/tmp/cockloaddb-slog-try/main.go", "name": "main.f1", "line": 12 }, "main.main": { "file": "/Users/nozo/tmp/cockloaddb-slog-try/main.go", "name": "main.main", "line": 33 }, "runtime.main": { "file": "/opt/homebrew/Cellar/go/1.21.1/libexec/src/runtime/proc.go", "name": "runtime.main", "line": 267 }, "runtime.goexit": { "file": "/opt/homebrew/Cellar/go/1.21.1/libexec/src/runtime/asm_arm64.s", "name": "runtime.goexit", "line": 1197 } } } } ```
cockroach-teamcity commented 10 months ago

CLA assistant check
All committers have signed the CLA.