-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwrap_test.go
58 lines (51 loc) · 1.45 KB
/
wrap_test.go
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
53
54
55
56
57
58
package structlog_test
import (
"bytes"
"fmt"
"io"
"os"
"testing"
"github.com/powerman/check"
"github.com/powerman/structlog"
)
func TestWrapErr(tt *testing.T) {
t := check.T(tt)
var buf bytes.Buffer
log := structlog.New().SetOutput(&buf)
err := log.WrapErr(io.EOF, "a", 10, "b", 20)
err = log.WrapErr(err, "a", 11, "c", 30)
err = log.WrapErr(err, "a", 12, "d", 40)
log.Warn("hmm", "c", 31, "e", 50, "err", err)
t.Match(buf.String(), "`hmm` a=12 b=20 c=31 d=40 e=50 err=EOF")
t.Nil(log.WrapErr(nil, "a", 10))
}
func ExampleLogger_WrapErr() {
// Use NewZeroLogger to avoid reconfiguring
// structlog.DefaultLogger in example, but in real code usually
// reconfiguring DefaultLogger is better than using NewZeroLogger.
log := structlog.NewZeroLogger().
SetOutput(os.Stdout).
SetPrefixKeys(structlog.KeyLevel).
SetKeysFormat(map[string]string{
structlog.KeyLevel: "%[2]s",
structlog.KeyMessage: " %#[2]q",
})
lowLevelFunc := func() error {
return log.WrapErr(io.EOF, "details", "about error")
}
middleLevelFunc := func(action string) error {
if err := lowLevelFunc(); err != nil {
err = fmt.Errorf("lowLevelFunc: %w", err)
return log.WrapErr(err, "action", action)
}
return nil
}
topLevelFunc := func() {
if err := middleLevelFunc("doit"); err != nil {
log.Warn("log only at top level", "err", err)
}
}
topLevelFunc()
// Output:
// WRN `log only at top level` details=about error action=doit err=lowLevelFunc: EOF
}