-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathexample_test.go
74 lines (60 loc) · 1.62 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
package logz_test
import (
"log"
"net/http"
"os"
"github.com/glassonion1/logz"
"github.com/glassonion1/logz/middleware"
)
func Example() {
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
// Writes info log
logz.Infof(ctx, "writes %s log", "info")
})
logz.SetProjectID("your gcp project id")
logz.InitTracer()
// Sets the middleware
h := middleware.NetHTTP("tracer name")(mux)
log.Fatal(http.ListenAndServe(":8080", h))
}
func ExampleSetConfig() {
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
// Writes info log
logz.Infof(ctx, "writes %s log", "info")
})
logz.SetConfig(logz.Config{
ProjectID: "your gcp project id",
NeedsAccessLog: false, // Whether or not to write the access log
})
logz.InitTracer()
// Sets the middleware
h := middleware.NetHTTP("tracer name")(mux)
log.Fatal(http.ListenAndServe(":8080", h))
}
func ExampleSetConfig_changeLogout() {
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
// Writes info log
logz.Infof(ctx, "writes %s log", "info")
})
// Writes log on local file
file, err := os.OpenFile("local.log", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0777)
if err != nil {
panic(err)
}
logz.SetConfig(logz.Config{
ProjectID: "your gcp project id",
NeedsAccessLog: false,
AccessLogOut: file,
ApplicationLogOut: file,
})
logz.InitTracer()
// Sets the middleware
h := middleware.NetHTTP("tracer name")(mux)
log.Fatal(http.ListenAndServe(":8080", h))
}