-
Notifications
You must be signed in to change notification settings - Fork 22
/
example_test.go
133 lines (101 loc) · 3.21 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package slog_test
import (
"context"
"io"
"net"
"os"
"testing"
"time"
"golang.org/x/xerrors"
"cdr.dev/slog"
"cdr.dev/slog/sloggers/sloghuman"
"cdr.dev/slog/sloggers/slogstackdriver"
"cdr.dev/slog/sloggers/slogtest"
)
func Example() {
log := slog.Make(sloghuman.Sink(os.Stdout))
log.Info(context.Background(), "my message here",
slog.F("field_name", "something or the other"),
slog.F("some_map", slog.M(
slog.F("nested_fields", time.Date(2000, time.February, 5, 4, 4, 4, 0, time.UTC)),
)),
slog.Error(
xerrors.Errorf("wrap1: %w",
xerrors.Errorf("wrap2: %w",
io.EOF,
),
),
),
)
// 2019-12-09 05:04:53.398 [INFO] <example.go:16> my message here {"field_name": "something or the other", "some_map": {"nested_fields": "2000-02-05T04:04:04Z"}} ...
// "error": wrap1:
// main.main
// /Users/nhooyr/src/cdr/scratch/example.go:22
// - wrap2:
// main.main
// /Users/nhooyr/src/cdr/scratch/example.go:23
// - EOF
}
func Example_struct() {
l := slog.Make(sloghuman.Sink(os.Stdout))
type hello struct {
Meow int `json:"meow"`
Bar string `json:"bar"`
M time.Time `json:"m"`
}
l.Info(context.Background(), "check out my structure",
slog.F("hello", hello{
Meow: 1,
Bar: "barbar",
M: time.Date(2000, time.February, 5, 4, 4, 4, 0, time.UTC),
}),
)
// 2019-12-16 17:31:51.769 [INFO] <example_test.go:56> check out my structure {"hello": {"meow": 1, "bar": "barbar", "m": "2000-02-05T04:04:04Z"}}
}
func Example_testing() {
// Provided by the testing package in tests.
var t testing.TB
slogtest.Info(t, "my message here",
slog.F("field_name", "something or the other"),
)
// t.go:55: 2019-12-05 21:20:31.218 [INFO] my message here field_name="something or the other"
}
func Example_multiple() {
l := slog.Make(sloghuman.Sink(os.Stdout))
f, err := os.OpenFile("stackdriver", os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0o644)
if err != nil {
l.Fatal(context.Background(), "failed to open stackdriver log file", slog.Error(err))
}
l = l.AppendSinks(slogstackdriver.Sink(f))
l.Info(context.Background(), "log to stdout and stackdriver")
// 2019-12-07 20:59:55.790 [INFO] log to stdout and stackdriver
}
func ExampleWith() {
ctx := slog.With(context.Background(), slog.F("field", 1))
l := slog.Make(sloghuman.Sink(os.Stdout))
l.Info(ctx, "msg")
// 2019-12-07 20:54:23.986 [INFO] msg field=1}
}
func ExampleStdlib() {
ctx := slog.With(context.Background(), slog.F("field", 1))
l := slog.Stdlib(ctx, slog.Make(sloghuman.Sink(os.Stdout)), slog.LevelInfo)
l.Print("msg")
// 2019-12-07 20:54:23.986 [INFO] (stdlib) msg field=1
}
func ExampleLogger_Named() {
ctx := context.Background()
l := slog.Make(sloghuman.Sink(os.Stdout))
l = l.Named("http")
l.Info(ctx, "received request", slog.F("remote address", net.IPv4(127, 0, 0, 1)))
// 2019-12-07 21:20:56.974 [INFO] (http) received request remote_address=127.0.0.1}
}
func ExampleLogger_Leveled() {
ctx := context.Background()
l := slog.Make(sloghuman.Sink(os.Stdout))
l.Debug(ctx, "testing1")
l.Info(ctx, "received request")
l = l.Leveled(slog.LevelDebug)
l.Debug(ctx, "testing2")
// 2019-12-07 21:26:20.945 [INFO] received request
// 2019-12-07 21:26:20.945 [DEBU] testing2
}