Skip to content

Commit

Permalink
Add slog.LogValuer interface
Browse files Browse the repository at this point in the history
  • Loading branch information
m-mizutani committed Aug 12, 2023
1 parent e6f257d commit f9cac8f
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 4 deletions.
28 changes: 28 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"io"
"strings"

"log/slog"

"github.com/google/uuid"
)

Expand Down Expand Up @@ -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...)
}
25 changes: 25 additions & 0 deletions examples/logging/main.go
Original file line number Diff line number Diff line change
@@ -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))
}
}
7 changes: 5 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -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
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
2 changes: 1 addition & 1 deletion go.work
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
go 1.18
go 1.21

use (
.
Expand Down
2 changes: 1 addition & 1 deletion test/go.mod
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit f9cac8f

Please sign in to comment.