-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample_test.go
77 lines (58 loc) · 1.65 KB
/
example_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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package kv_test
import (
"context"
"fmt"
"strings"
"github.com/jjeffery/kv"
)
func init() {
// Use fmt.Println for logging messages so that Output works
// in example test cases.
kv.LogOutput = func(calldepth int, s string) error {
_, err := fmt.Println(strings.TrimSpace(s))
return err
}
}
func ExampleContext() {
// start with a context
ctx := context.Background()
// attach some key/value pairs
ctx = kv.From(ctx).With("method", "get", "url", "/api/widgets")
// ... later on log a message ...
fmt.Println("permission denied", kv.From(ctx).With("user", "alice"))
// Output:
// permission denied user=alice method=get url="/api/widgets"
}
func ExampleError() {
// setup the user
user := "alice"
// ... later on ...
// create an error and log it
err := kv.NewError("permission denied").With("user", user)
fmt.Println(err)
// alternatively, wrap an existing error
err = kv.Wrap(err, "cannot open file").With("file", "/etc/passwd")
fmt.Println(err)
// Output:
// permission denied user=alice
// cannot open file: permission denied file="/etc/passwd" user=alice
}
func ExampleLog() {
kv.Log("message 1")
kv.Log("message 2", kv.With("a", 1))
kv := kv.With("p", "q")
kv.Log("message 3")
kv.Log("message 4", kv.With("a", 1, "b", 2))
ctx := kv.From(context.Background()).With("c1", 100, "c2", 200)
kv.Log(ctx, "message 5")
kv.Log("message 6:", ctx, "can be in any order")
kv.Log(ctx, "message 7", kv.With("a", 1, "b", 2))
// Output:
// message 1
// message 2 a=1
// message 3 p=q
// message 4 p=q a=1 b=2
// message 5 p=q c1=100 c2=200
// message 6: can be in any order p=q c1=100 c2=200
// message 7 p=q a=1 b=2 c1=100 c2=200
}