forked from yfuruyama/stackdriver-request-context-log
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstackdriver.go
319 lines (265 loc) · 8.51 KB
/
stackdriver.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
// Package stalog provides application logger for Cloud Logging.
package stalog
import (
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"runtime"
"strings"
"time"
)
type AdditionalData map[string]interface{}
// Config is the configuration for `RequestLogging` middleware.
type Config struct {
ProjectId string
// Output for request log
RequestLogOut io.Writer
// Output for context log (application log)
ContextLogOut io.Writer
Severity Severity
AdditionalData AdditionalData
// nest level for runtime.Caller (default: 2)
Skip int
}
// NewConfig creates a config with default settings.
func NewConfig(projectId string) *Config {
return &Config{
ProjectId: projectId,
Severity: SeverityInfo,
RequestLogOut: os.Stderr,
ContextLogOut: os.Stdout,
AdditionalData: AdditionalData{},
Skip: 2,
}
}
// Severity is the level of log. More details:
// https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry#LogSeverity
type Severity int
const (
SeverityDefault Severity = iota * 100
SeverityDebug
SeverityInfo
SeverityNotice
SeverityWarning
SeverityError
SeverityCritical
SeverityAlert
SeverityEmergency
)
// String returns text representation for the severity
func (s Severity) String() string {
switch s {
case SeverityDefault:
return "DEFAULT"
case SeverityDebug:
return "DEBUG"
case SeverityInfo:
return "INFO"
case SeverityNotice:
return "NOTICE"
case SeverityWarning:
return "WARNING"
case SeverityError:
return "ERROR"
case SeverityCritical:
return "CRITICAL"
case SeverityAlert:
return "ALERT"
case SeverityEmergency:
return "EMERGENCY"
default:
return "UNKNOWN"
}
}
type SourceLocation struct {
File string `json:"file"`
Line string `json:"line"`
Function string `json:"function"`
}
type contextLog struct {
Time string `json:"time"`
Trace string `json:"logging.googleapis.com/trace"`
SourceLocation SourceLocation `json:"logging.googleapis.com/sourceLocation"`
Severity string `json:"severity"`
Message string `json:"message"`
AdditionalData AdditionalData `json:"data,omitempty"`
}
// ContextLogger is the logger which is combined with the request
type ContextLogger struct {
out io.Writer
Trace string
Severity Severity
AdditionalData AdditionalData
loggedSeverity []Severity
Skip int
}
// RequestContextLogger gets request-context logger for the request.
// You must use `RequestLogging` middleware in advance for this function to work.
func RequestContextLogger(r *http.Request) *ContextLogger {
v, _ := r.Context().Value(ContextLoggerKey).(*ContextLogger)
return v
}
// Default logs a message at DEFAULT severity
func (l *ContextLogger) Default(args ...interface{}) {
_ = l.write(SeverityDefault, fmt.Sprint(args...))
}
// Defaultf logs a message at DEFAULT severity
func (l *ContextLogger) Defaultf(format string, args ...interface{}) {
_ = l.write(SeverityDefault, fmt.Sprintf(format, args...))
}
// Defaultln logs a message at DEFAULT severity
func (l *ContextLogger) Defaultln(args ...interface{}) {
_ = l.write(SeverityDefault, fmt.Sprintln(args...))
}
// Debug logs a message at DEBUG severity
func (l *ContextLogger) Debug(args ...interface{}) {
_ = l.write(SeverityDebug, fmt.Sprint(args...))
}
// Debugf logs a message at DEBUG severity
func (l *ContextLogger) Debugf(format string, args ...interface{}) {
_ = l.write(SeverityDebug, fmt.Sprintf(format, args...))
}
// Debugln logs a message at DEBUG severity
func (l *ContextLogger) Debugln(args ...interface{}) {
_ = l.write(SeverityDebug, fmt.Sprintln(args...))
}
// Info logs a message at INFO severity
func (l *ContextLogger) Info(args ...interface{}) {
_ = l.write(SeverityInfo, fmt.Sprint(args...))
}
// Infof logs a message at INFO severity
func (l *ContextLogger) Infof(format string, args ...interface{}) {
_ = l.write(SeverityInfo, fmt.Sprintf(format, args...))
}
// Infoln logs a message at INFO severity
func (l *ContextLogger) Infoln(args ...interface{}) {
_ = l.write(SeverityInfo, fmt.Sprintln(args...))
}
// Notice logs a message at NOTICE severity
func (l *ContextLogger) Notice(args ...interface{}) {
_ = l.write(SeverityNotice, fmt.Sprint(args...))
}
// Noticef logs a message at NOTICE severity
func (l *ContextLogger) Noticef(format string, args ...interface{}) {
_ = l.write(SeverityNotice, fmt.Sprintf(format, args...))
}
// Noticeln logs a message at NOTICE severity
func (l *ContextLogger) Noticeln(args ...interface{}) {
_ = l.write(SeverityNotice, fmt.Sprintln(args...))
}
// Warning logs a message at WARNING severity
func (l *ContextLogger) Warning(args ...interface{}) {
_ = l.write(SeverityWarning, fmt.Sprint(args...))
}
// Warningf logs a message at WARNING severity
func (l *ContextLogger) Warningf(format string, args ...interface{}) {
_ = l.write(SeverityWarning, fmt.Sprintf(format, args...))
}
// Warningln logs a message at WARNING severity
func (l *ContextLogger) Warningln(args ...interface{}) {
_ = l.write(SeverityWarning, fmt.Sprintln(args...))
}
// Warn logs a message at WARNING severity
func (l *ContextLogger) Warn(args ...interface{}) {
_ = l.write(SeverityWarning, fmt.Sprint(args...))
}
// Warnf logs a message at WARNING severity
func (l *ContextLogger) Warnf(format string, args ...interface{}) {
_ = l.write(SeverityWarning, fmt.Sprintf(format, args...))
}
// Warnln logs a message at WARNING severity
func (l *ContextLogger) Warnln(args ...interface{}) {
_ = l.write(SeverityWarning, fmt.Sprintln(args...))
}
// Error logs a message at ERROR severity
func (l *ContextLogger) Error(args ...interface{}) {
_ = l.write(SeverityError, fmt.Sprint(args...))
}
// Errorf logs a message at ERROR severity
func (l *ContextLogger) Errorf(format string, args ...interface{}) {
_ = l.write(SeverityError, fmt.Sprintf(format, args...))
}
// Errorln logs a message at ERROR severity
func (l *ContextLogger) Errorln(args ...interface{}) {
_ = l.write(SeverityError, fmt.Sprintln(args...))
}
// Critical logs a message at CRITICAL severity
func (l *ContextLogger) Critical(args ...interface{}) {
_ = l.write(SeverityCritical, fmt.Sprint(args...))
}
// Criticalf logs a message at CRITICAL severity
func (l *ContextLogger) Criticalf(format string, args ...interface{}) {
_ = l.write(SeverityCritical, fmt.Sprintf(format, args...))
}
// Criticalln logs a message at CRITICAL severity
func (l *ContextLogger) Criticalln(args ...interface{}) {
_ = l.write(SeverityCritical, fmt.Sprintln(args...))
}
// Alert logs a message at ALERT severity
func (l *ContextLogger) Alert(args ...interface{}) {
_ = l.write(SeverityAlert, fmt.Sprint(args...))
}
// Alertf logs a message at ALERT severity
func (l *ContextLogger) Alertf(format string, args ...interface{}) {
_ = l.write(SeverityAlert, fmt.Sprintf(format, args...))
}
// Alertln logs a message at ALERT severity
func (l *ContextLogger) Alertln(args ...interface{}) {
_ = l.write(SeverityAlert, fmt.Sprintln(args...))
}
// Emergency logs a message at EMERGENCY severity
func (l *ContextLogger) Emergency(args ...interface{}) {
_ = l.write(SeverityEmergency, fmt.Sprint(args...))
}
// Emergencyf logs a message at EMERGENCY severity
func (l *ContextLogger) Emergencyf(format string, args ...interface{}) {
_ = l.write(SeverityEmergency, fmt.Sprintf(format, args...))
}
// Emergencyln logs a message at EMERGENCY severity
func (l *ContextLogger) Emergencyln(args ...interface{}) {
_ = l.write(SeverityEmergency, fmt.Sprintln(args...))
}
func (l *ContextLogger) write(severity Severity, msg string) error {
if severity < l.Severity {
return nil
}
l.loggedSeverity = append(l.loggedSeverity, severity)
// get source location
var location SourceLocation
if pc, file, line, ok := runtime.Caller(l.Skip); ok {
if function := runtime.FuncForPC(pc); function != nil {
location.Function = function.Name()
}
location.Line = fmt.Sprintf("%d", line)
parts := strings.Split(file, "/")
location.File = parts[len(parts)-1] // use short file name
}
log := &contextLog{
Time: time.Now().Format(time.RFC3339Nano),
Trace: l.Trace,
SourceLocation: location,
Severity: severity.String(),
Message: msg,
AdditionalData: l.AdditionalData,
}
jsonByte, err := json.Marshal(log)
if err != nil {
_, _ = fmt.Fprintln(os.Stderr, err.Error())
return err
}
// append \n
jsonByte = append(jsonByte, 0xa)
_, err = l.out.Write(jsonByte)
return err
}
func (l *ContextLogger) maxSeverity() Severity {
max := SeverityDefault
for _, s := range l.loggedSeverity {
if s > max {
max = s
}
}
return max
}