-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
52 lines (40 loc) · 1.34 KB
/
example_test.go
File metadata and controls
52 lines (40 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package errors_test
import (
"context"
"fmt"
"github.com/kapetan-io/errors"
"log/slog"
)
type Struct struct {
attrs *errors.Attrs
}
func ExampleAttrs() {
// Attach context to an error
err := errors.With("foo", "bar").Error("query failed")
// Prints `query failed`
fmt.Printf("%s\n", err)
// Prints `query failed (foo=bar)`
fmt.Printf("%+v\n", err)
// Prints `Attributes [foo=bar]`
fmt.Printf("Attributes %v\n", errors.AttrsFrom(err))
// Prints `2024/09/30 12:00:00 level=ERROR msg="query failed" foo=bar`
slog.LogAttrs(context.Background(), slog.LevelError, err.Error(), errors.AttrsFrom(err)...)
// Use with slog.Attr to create complex logging context
s := Struct{
attrs: errors.WithAttr(slog.String("database", "sqlite")),
}
// Prints `query failed (database=sqlite, foo=bar)`
fmt.Printf("%+v\n", s.attrs.With("foo", "bar").Error("query failed"))
// Which can be used to attach to logging
slog.LogAttrs(context.Background(), slog.LevelError, err.Error(), errors.AttrsFromWithCodeLoc(err)...)
// Prints `message: query error (key1=value1)`
err = errors.New("query error")
wrap := errors.With("key1", "value1").Errorf("message: %w", err)
fmt.Printf("%+v\n", wrap)
// Output:
// query failed
// query failed (foo=bar)
// Attributes [foo=bar]
// query failed (database=sqlite, foo=bar)
// message: query error (key1=value1)
}