From f9cac8fbe028511c73c4eddb5fe8aad25dd2557a Mon Sep 17 00:00:00 2001 From: Masayoshi Mizutani Date: Sat, 12 Aug 2023 12:50:50 +0900 Subject: [PATCH] Add slog.LogValuer interface --- errors.go | 28 ++++++++++++++++++++++++++++ examples/logging/main.go | 25 +++++++++++++++++++++++++ go.mod | 7 +++++-- go.sum | 2 ++ go.work | 2 +- test/go.mod | 2 +- 6 files changed, 62 insertions(+), 4 deletions(-) create mode 100644 examples/logging/main.go diff --git a/errors.go b/errors.go index 21e542c..bdcc788 100644 --- a/errors.go +++ b/errors.go @@ -6,6 +6,8 @@ import ( "io" "strings" + "log/slog" + "github.com/google/uuid" ) @@ -182,3 +184,29 @@ func (x *Error) Values() map[string]any { return values } + +func (x *Error) LogValue() slog.Value { + attrs := []slog.Attr{ + slog.String("message", x.msg), + } + var values []any + for k, v := range x.values { + values = append(values, slog.Any(k, v)) + } + attrs = append(attrs, slog.Group("values", values...)) + + var stacktrace any + var traces []string + for _, st := range x.StackTrace() { + traces = append(traces, fmt.Sprintf("%s:%d %s", st.file(), st.line(), st.name())) + } + stacktrace = traces + + attrs = append(attrs, slog.Any("stacktrace", stacktrace)) + + if x.cause != nil { + attrs = append(attrs, slog.Any("cause", x.cause)) + } + + return slog.GroupValue(attrs...) +} diff --git a/examples/logging/main.go b/examples/logging/main.go new file mode 100644 index 0000000..9470c3c --- /dev/null +++ b/examples/logging/main.go @@ -0,0 +1,25 @@ +package main + +import ( + "os" + + "log/slog" + + "github.com/m-mizutani/goerr" +) + +var runtimeError = goerr.New("runtime error") + +func someAction(input string) error { + if input != "OK" { + return goerr.Wrap(runtimeError, "input is not OK").With("input", input) + } + return nil +} + +func main() { + logger := slog.New(slog.NewJSONHandler(os.Stdout, nil)) + if err := someAction("ng"); err != nil { + logger.Error("fail someAction", slog.Any("error", err)) + } +} diff --git a/go.mod b/go.mod index a61b381..6717d70 100644 --- a/go.mod +++ b/go.mod @@ -1,5 +1,8 @@ module github.com/m-mizutani/goerr -go 1.18 +go 1.21 -require github.com/google/uuid v1.3.0 +require ( + github.com/google/uuid v1.3.0 + golang.org/x/exp v0.0.0-20230810033253-352e893a4cad +) diff --git a/go.sum b/go.sum index 3dfe1c9..9e1e4ba 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,4 @@ github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +golang.org/x/exp v0.0.0-20230810033253-352e893a4cad h1:g0bG7Z4uG+OgH2QDODnjp6ggkk1bJDsINcuWmJN1iJU= +golang.org/x/exp v0.0.0-20230810033253-352e893a4cad/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc= diff --git a/go.work b/go.work index c3d2fb0..dc94c8f 100644 --- a/go.work +++ b/go.work @@ -1,4 +1,4 @@ -go 1.18 +go 1.21 use ( . diff --git a/test/go.mod b/test/go.mod index f9eae03..5bd59ce 100644 --- a/test/go.mod +++ b/test/go.mod @@ -1,6 +1,6 @@ module github.com/m-mizutani/goerr/test -go 1.18 +go 1.21 require ( github.com/m-mizutani/goerr v0.1.5