forked from nownabe/clog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_test.go
217 lines (193 loc) · 6.27 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package clog_test
import (
"context"
"log/slog"
"net/http"
"net/http/httptest"
"os"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/trace"
"go.nownabe.dev/clog"
"go.nownabe.dev/clog/errors"
)
type ctxUserIDKey struct{}
func InitializeClog() {
// You can add labels to classify logs.
appLabels := map[string]string{
"app": "myapp",
"version": "1.0.0",
}
// Your Google Cloud Project ID.
projectID := "my-gcp-project"
// Custome handler to add user ID to logs.
customHandler := func(next clog.HandleFunc) clog.HandleFunc {
return func(ctx context.Context, r slog.Record) error {
if ctx == nil {
return next(ctx, r)
}
if userID, ok := ctx.Value(ctxUserIDKey{}).(string); ok {
r.AddAttrs(slog.String("user_id", userID))
}
return next(ctx, r)
}
}
// Create a custom logger.
logger := clog.New(os.Stdout, clog.SeverityInfo, true,
clog.WithLabels(appLabels), // Add logger-level labels to classify logs.
clog.WithTrace(projectID), // Add trace and span ID to logs.
clog.WithHandleFunc(customHandler), // Add custom log handler.
)
// You can add custome fields to logs.
logger = logger.With("key1", "value1", "key2", "value2")
clog.SetDefault(logger)
}
func RequestHandler(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
tracer := otel.Tracer("tracer")
ctx, span := tracer.Start(ctx, "RequestHandler")
defer span.End()
ctx, removeLabel := clog.ContextWithLabel(ctx, "handler", "RequestHandler")
defer removeLabel()
clog.Info(ctx, "request received")
/*
{
"time": "2023-09-18T20:46:03.448753224+09:00",
"severity": "INFO",
"message": "request received",
"logging.googleapis.com/sourceLocation": {
"file": "go.nownabe.dev/clog/example_test.go",
"line": "66",
"function": "main.RequestHandler"
},
"user_id": "user1",
"logging.googleapis.com/trace": "projects/my-gcp-project/traces/abcdabcdabcdabcdabcdabcdabcdabcd",
"logging.googleapis.com/spanId": "1234123412341234",
"logging.googleapis.com/trace_sampled": false,
"logging.googleapis.com/labels": {
"handler": "RequestHandler",
"app": "myapp",
"version": "1.0.0"
}
}
*/
defer clog.Info(ctx, "request processed")
err := errors.New("something went wrong")
if err != nil {
clog.Err(ctx, err)
/*
{
"time": "2023-09-18T21:14:02.980412394+09:00",
"severity": "ERROR",
"message": "something went wrong",
"stack_trace": "something went wrong\n\ngoroutine 0 [running]:\nmain.RequestHandler(...)\n\tgo.nownabe.dev/clog/example_test.go:90\nnet/http.HandlerFunc.ServeHTTP(...)\n\tnet/http/server.go:2136\nmain.Example(...)\n\tgo.nownabe.dev/clog/example_test.go:135\nmain.main(...)\n\tgo.nownabe.dev/clog/main.go:20\nruntime.main(...)\n\truntime/proc.go:267\n",
"logging.googleapis.com/sourceLocation": {
"file": "go.nownabe.dev/clog/example_test.go",
"line": "92",
"function": "main.RequestHandler"
},
"user_id": "user1",
"logging.googleapis.com/trace": "projects/my-gcp-project/traces/abcdabcdabcdabcdabcdabcdabcdabcd",
"logging.googleapis.com/spanId": "1234123412341234",
"logging.googleapis.com/trace_sampled": false,
"logging.googleapis.com/labels": {
"handler": "RequestHandler",
"app": "myapp",
"version": "1.0.0"
}
}
*/
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
/*
{
"time": "2023-09-18T21:14:02.980418868+09:00",
"severity": "INFO",
"message": "request processed",
"logging.googleapis.com/sourceLocation": {
"file": "go.nownabe.dev/clog/example_test.go",
"line": "116",
"function": "main.RequestHandler"
},
"user_id": "user1",
"logging.googleapis.com/trace": "projects/my-gcp-project/traces/abcdabcdabcdabcdabcdabcdabcdabcd",
"logging.googleapis.com/spanId": "1234123412341234",
"logging.googleapis.com/trace_sampled": false,
"logging.googleapis.com/labels": {
"handler": "RequestHandler",
"app": "myapp",
"version": "1.0.0"
}
}
*/
}
w.Write([]byte("Hello, World!"))
}
func Example() {
ctx := context.Background()
ctx = withTraceSpan(ctx)
ctx = context.WithValue(ctx, ctxUserIDKey{}, "user1")
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "", nil)
if err != nil {
panic(err)
}
InitializeClog()
clog.Notice(ctx, "app started")
/*
{
"time": "2023-09-18T20:46:03.448702881+09:00",
"severity": "NOTICE",
"message": "app started",
"logging.googleapis.com/sourceLocation": {
"file": "go.nownabe.dev/clog/example_test.go",
"line": "92",
"function": "main.Example"
},
"user_id": "user1",
"logging.googleapis.com/trace": "projects/my-gcp-project/traces/abcdabcdabcdabcdabcdabcdabcdabcd",
"logging.googleapis.com/spanId": "1234123412341234",
"logging.googleapis.com/trace_sampled": false,
"logging.googleapis.com/labels": {
"app": "myapp",
"version": "1.0.0"
}
}
*/
(http.HandlerFunc(RequestHandler)).ServeHTTP(httptest.NewRecorder(), req)
clog.Notice(ctx, "app finished")
/*
{
"time": "2023-09-18T21:14:02.980422814+09:00",
"severity": "NOTICE",
"message": "app finished",
"logging.googleapis.com/sourceLocation": {
"file": "go.nownabe.dev/clog/tmp/example.go",
"line": "178",
"function": "main.Example"
},
"user_id": "user1",
"logging.googleapis.com/trace": "projects/my-gcp-project/traces/abcdabcdabcdabcdabcdabcdabcdabcd",
"logging.googleapis.com/spanId": "1234123412341234",
"logging.googleapis.com/trace_sampled": false,
"logging.googleapis.com/labels": {
"app": "myapp",
"version": "1.0.0"
}
}
*/
}
func withTraceSpan(ctx context.Context) context.Context {
traceID, err := trace.TraceIDFromHex("abcdabcdabcdabcdabcdabcdabcdabcd")
if err != nil {
panic(err)
}
spanID, err := trace.SpanIDFromHex("1234123412341234")
if err != nil {
panic(err)
}
cfg := trace.SpanContextConfig{
TraceID: traceID,
SpanID: spanID,
}
sc := trace.NewSpanContext(cfg)
return trace.ContextWithSpanContext(ctx, sc)
}