This repository has been archived by the owner on Apr 11, 2019. It is now read-only.
/
example2_test.go
108 lines (82 loc) · 2.4 KB
/
example2_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
package slog_test
import (
"log"
"net/http"
"github.com/spkg/slog"
"golang.org/x/net/context"
)
type ExternalHandler struct {
// ... details for accessing external handler ...
}
func (h *ExternalHandler) Handle(msgs []*slog.Message) {
// ... send to msgs to external logging handler ...
}
func ExampleAddHandler() {
slog.AddHandler(&ExternalHandler{})
}
func ExampleNewWriter(ctx context.Context) {
// Creates a HTTP server whose error log will write to the
// default slog.Logger. Any panics that are recovered will
// have the details logged via slog.
httpServer := &http.Server{
Addr: ":8080",
ErrorLog: log.New(slog.NewWriter(ctx), "http", 0),
}
slog.Info(ctx, "web server started", slog.WithValue("addr", httpServer.Addr))
if err := httpServer.ListenAndServe(); err != nil {
slog.Error(ctx, "web server failed", slog.WithError(err))
}
// 2009-11-10T12:34:56.789 info msg="web server started" addr=:8080
// 2009-11-10T12:35:57:987 error msg="http: panic serving 123.1:2.3:36145 runtime error: invalid memory address or nil pointer dereference"
}
func ExampleOption(ctx context.Context, n1, n2 int) error {
if err := doSomethingWith(n1, n2); err != nil {
return slog.Error(ctx, "cannot doSomething",
slog.WithValue("n1", n1),
slog.WithValue("n2", n2),
slog.WithError(err))
}
// .. more processing and then ...
return nil
}
func ExampleWithValue(ctx context.Context, n1, n2 int) error {
if err := doSomethingWith(n1, n2); err != nil {
return slog.Error(ctx, "doSomethingWith failed",
slog.WithValue("n1", n1),
slog.WithValue("n2", n2))
}
// ... more processing and then ...
return nil
}
func ExampleWithError(ctx context.Context) error {
if err := doSomething(); err != nil {
return slog.Error(ctx, "doSomething failed",
slog.WithError(err))
}
// ... more processing and then ...
return nil
}
func ExampleNewContext(ctx context.Context, n1, n2 int) error {
ctx = slog.NewContext(ctx,
slog.Property{Key: "n1", Value: n1},
slog.Property{Key: "n2", Value: n2})
if err := doSomethingWith(n1, n2); err != nil {
return slog.Error(ctx, "doSomethingWith failed",
slog.WithError(err))
}
slog.Debug(ctx, "did something with")
// ... more processing and then ...
return nil
}
func doSomethingWith(n1 int, n2 int) error {
return nil
}
func doSomething() error {
return nil
}
func doAnotherThing() error {
return nil
}
func doOneMoreThing() error {
return nil
}