/
log_msg.go
446 lines (361 loc) · 10.6 KB
/
log_msg.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
// Copyright 2021-2022, the SS project owners. All rights reserved.
// Please see the OWNERS and LICENSE files for details.
package ss
import (
"bufio"
"encoding/json"
"fmt"
"runtime"
"strings"
"time"
)
const (
logMsgNodeTime = "time"
logMsgNodeMessage = "message"
logMsgNodeLevel = "level"
logMsgNodeErrorList = "err"
logMsgNodeUser = "user"
logMsgNodeConnection = "connection"
logMsgNodeRequest = "request"
logMsgNodeDumpList = "dump"
logMsgNodeDumpGroupList = "dumpGroup"
logMsgNodeDumpGroupRequestList = "request"
logMsgNodeDumpGroupResponseList = "response"
logMsgNodeStack = "stack"
logMsgNodeMarshalError = "marshalErr"
)
////////////////////////////////////////////////////////////////////////////////
type LogMsg struct {
message string
parents []*LogMsg
attributes []LogMsgAttr
level logLevel
errs []logMsgAttrErr
time time.Time
}
func NewLogMsg(format string, args ...interface{}) *LogMsg {
return &LogMsg{
message: fmt.Sprintf(format, args...),
time: Now().Get(),
}
}
func (m *LogMsg) AddInfoPrefix(prefix LogPrefix) *LogMsg {
m.AddAttrs(prefix.generalAttrs)
return m
}
func (m *LogMsg) AddFailPrefix(prefix LogPrefix) *LogMsg {
m.AddInfoPrefix(prefix)
m.AddAttrs(prefix.newFailAttrs())
return m
}
func (m *LogMsg) AddAttrs(source []LogMsgAttr) *LogMsg {
m.attributes = append(m.attributes, source...)
return m
}
func (m *LogMsg) AddParent(source *LogMsg) {
// It will not use level and time from source message, as an error already
// happened and info from this occurring is more important.
m.parents = append(m.parents, source)
m.errs = append(m.errs, source.errs...)
}
func (m *LogMsg) Add(source LogMsgAttr) *LogMsg {
m.attributes = append(m.attributes, source)
return m
}
func (m *LogMsg) AddRequest(source interface{}) *LogMsg {
m.attributes = append(m.attributes, newLogMsgAttrRequestDump(source))
return m
}
func (m *LogMsg) AddResponse(source interface{}) *LogMsg {
m.attributes = append(
m.attributes,
NewLogMsgAttrDumpGroup(logMsgNodeDumpGroupResponseList, source))
return m
}
func (m *LogMsg) AddErr(source error) *LogMsg {
m.errs = append(m.errs, newLogMsgAttrError(source))
return m
}
func (m *LogMsg) AddPanic(source interface{}) *LogMsg {
m.errs = append(m.errs, newLogMsgAttrPanic(source))
return m
}
func (m *LogMsg) AddRequestID(source string) *LogMsg {
return m.AddVal(logMsgNodeConnection, source)
}
func (m *LogMsg) AddDump(source interface{}) *LogMsg {
m.attributes = append(m.attributes, newLogMsgAttrDump(source))
return m
}
func (m *LogMsg) AddVal(name string, value interface{}) *LogMsg {
m.attributes = append(m.attributes, NewLogMsgAttrVal(name, value))
return m
}
func (m *LogMsg) AddCurrentStack() *LogMsg {
m.attributes = append(m.attributes, newLogMsgAttrCurrentStack())
return m
}
func (m *LogMsg) SetLevel(source logLevel) { m.level = source }
func (m LogMsg) GetLevel() logLevel { return m.level }
func (m LogMsg) GetErrs() []logMsgAttrErr { return m.errs }
func (m LogMsg) GetMessage() string {
crateNodeMessage := func(node LogMsg) string {
result := node.message
if node.errs != nil {
result += ":"
for i, err := range node.errs {
result += fmt.Sprintf(" %d) %s;", i+1, err.Get())
}
}
return result
}
result := crateNodeMessage(m)
var levels []string
var addParent func(parent LogMsg, level int)
var addParents func(parent LogMsg)
addParent = func(node LogMsg, level int) {
levels = append(levels, fmt.Sprintf("%d", level))
result += fmt.Sprintf(
"\n%s: %s",
strings.Join(levels, "."),
crateNodeMessage(node))
addParents(node)
levels = levels[:len(levels)-1]
}
addParents = func(node LogMsg) {
for i, parent := range node.parents {
addParent(*parent, i+1)
}
}
addParents(m)
return result
}
func (m LogMsg) Error() string {
return m.GetMessage() + " " + string(m.ConvertAttributesToJSON())
}
func (m LogMsg) MarshalMap() map[string]interface{} {
result := m.MarshalAttributesMap()
result[logMsgNodeMessage] = m.GetMessage()
result[logMsgNodeLevel] = m.level
return result
}
func (m LogMsg) ConvertToJSON() []byte {
value := m.MarshalMap()
result, err := json.Marshal(value)
if err != nil {
result = []byte(
fmt.Sprintf(
`{%q:{"err":%q,"value":%q}}`,
logMsgNodeMarshalError,
fmt.Sprintf("%v", err),
fmt.Sprintf("%v", value)))
}
return result
}
func (m LogMsg) MarshalAttributesMap() map[string]interface{} {
result := map[string]interface{}{
logMsgNodeTime: m.time,
}
var errs []interface{}
var add func(node LogMsg)
add = func(node LogMsg) {
for _, a := range node.attributes {
a.MarshalLogMsg(result)
}
for _, e := range node.errs {
errs = append(errs, e.MarshalLogMsg())
}
for _, p := range node.parents {
add(*p)
}
}
add(m)
if errs != nil {
result[logMsgNodeErrorList] = errs
}
return result
}
func (m LogMsg) ConvertAttributesToJSON() []byte {
value := m.MarshalAttributesMap()
result, err := json.Marshal(value)
if err != nil {
result = []byte(
fmt.Sprintf(
`{%q:{"err":%q,"value":%q}}`,
logMsgNodeMarshalError,
fmt.Sprintf("%v", err),
fmt.Sprintf("%#v", value)))
}
return result
}
////////////////////////////////////////////////////////////////////////////////
type LogMsgAttr interface {
MarshalLogMsg(destination map[string]interface{})
}
////////////////////////////////////////////////////////////////////////////////
type LogMsgAttrVal struct {
node string
value interface{}
}
func NewLogMsgAttrVal(node string, value interface{}) LogMsgAttrVal {
return LogMsgAttrVal{
node: node,
value: value,
}
}
func (a LogMsgAttrVal) MarshalLogMsg(destination map[string]interface{}) {
destination[a.node] = a.value
}
////////////////////////////////////////////////////////////////////////////////
type logMsgAttrDump struct{ value interface{} }
func newLogMsgAttrDump(value interface{}) logMsgAttrDump {
return logMsgAttrDump{value: value}
}
func MarshalLogMsgAttrDump(
value interface{},
destination map[string]interface{},
) {
marshalValue := logMsgAttrDumpValue{newLogMsgValueTypeName(value): value}
if node, has := destination[logMsgNodeDumpList]; has {
destination[logMsgNodeDumpList] = append(
node.([]logMsgAttrDumpValue),
marshalValue)
return
}
destination[logMsgNodeDumpList] = []logMsgAttrDumpValue{marshalValue}
}
func (a logMsgAttrDump) MarshalLogMsg(destination map[string]interface{}) {
MarshalLogMsgAttrDump(a.value, destination)
}
type logMsgAttrDumpValue map[string]interface{}
type LogMsgAttrDumpGroup struct {
logMsgAttrDump
groupNode string
}
func NewLogMsgAttrDumpGroup(
groupNode string,
value interface{},
) LogMsgAttrDumpGroup {
return LogMsgAttrDumpGroup{
logMsgAttrDump: newLogMsgAttrDump(value),
groupNode: groupNode,
}
}
func (a LogMsgAttrDumpGroup) MarshalLogMsg(destination map[string]interface{}) {
value := logMsgAttrDumpValue{newLogMsgValueTypeName(a.value): a.value}
if dumps, has := destination[logMsgNodeDumpGroupList]; has {
destination := dumps.(map[string]interface{})
if node, has := destination[a.groupNode]; has {
destination[a.groupNode] = append(
node.([]logMsgAttrDumpValue),
value)
return
}
destination[a.groupNode] = []logMsgAttrDumpValue{value}
return
}
destination[logMsgNodeDumpGroupList] = map[string]interface{}{
a.groupNode: []logMsgAttrDumpValue{value},
}
}
////////////////////////////////////////////////////////////////////////////////
func NewLogMsgAttrRequestDumps(value interface{}) []LogMsgAttr {
return []LogMsgAttr{newLogMsgAttrRequestDump(value)}
}
func newLogMsgAttrRequestDump(value interface{}) LogMsgAttr {
return NewLogMsgAttrDumpGroup(logMsgNodeDumpGroupRequestList, value)
}
////////////////////////////////////////////////////////////////////////////////
type logMsgAttrErr interface {
Get() error
MarshalLogMsg() interface{}
}
type logMsgAttrError struct{ value error }
func newLogMsgAttrError(source error) logMsgAttrErr {
return logMsgAttrError{value: source}
}
func (a logMsgAttrError) Get() error { return a.value }
func (a logMsgAttrError) MarshalLogMsg() interface{} {
return struct {
Type string `json:"type"`
Value string `json:"value"`
}{
Type: newLogMsgValueTypeName(a.value),
Value: a.Get().Error(),
}
}
type logMsgAttrPanic struct{ value logMsgAttrPanicValue }
func newLogMsgAttrPanic(source interface{}) logMsgAttrErr {
return logMsgAttrPanic{value: logMsgAttrPanicValue{value: source}}
}
func (a logMsgAttrPanic) Get() error { return a.value }
func (a logMsgAttrPanic) MarshalLogMsg() interface{} {
return struct {
Type string `json:"type"`
Value string `json:"value"`
}{
Type: newLogMsgValueTypeName(a.value),
Value: a.Get().Error(),
}
}
type logMsgAttrPanicValue struct{ value interface{} }
func (v logMsgAttrPanicValue) Error() string {
return fmt.Sprintf("%v", v.value)
}
////////////////////////////////////////////////////////////////////////////////
type logMsgAttrStack []interface{}
func newLogMsgAttrCurrentStack() logMsgAttrStack {
buffer := make([]byte, 4096)
size := runtime.Stack(buffer, false)
scanner := bufio.NewScanner(strings.NewReader(string(buffer[:size])))
stack := []interface{}{}
isFirst := true
for scanner.Scan() {
if isFirst {
// Skipping "goroutine 1 [running]:"...
isFirst = false
continue
}
level := struct {
Func string `json:"func"`
Path string `json:"path"`
}{
Func: scanner.Text(),
}
if scanner.Scan() {
level.Path = strings.Trim(scanner.Text(), "\t\n\r ")
}
stack = append(stack, level)
}
return logMsgAttrStack(stack)
}
func (a logMsgAttrStack) MarshalLogMsg(destination map[string]interface{}) {
destination[logMsgNodeStack] = a
}
////////////////////////////////////////////////////////////////////////////////
type LogPrefix struct {
generalAttrs []LogMsgAttr
newFailAttrs func() []LogMsgAttr
}
func NewLogPrefix(newFailAttrs func() []LogMsgAttr) LogPrefix {
return LogPrefix{
generalAttrs: make([]LogMsgAttr, 0, 1),
newFailAttrs: newFailAttrs,
}
}
func (lp LogPrefix) Add(a LogMsgAttr) LogPrefix {
lp.generalAttrs = append(lp.generalAttrs, a)
return lp
}
func (lp LogPrefix) AddRequestID(source string) LogPrefix {
return lp.AddVal(logMsgNodeRequest, source)
}
func (lp LogPrefix) AddVal(name string, value interface{}) LogPrefix {
lp.generalAttrs = append(lp.generalAttrs, NewLogMsgAttrVal(name, value))
return lp
}
////////////////////////////////////////////////////////////////////////////////
func newLogMsgValueTypeName(source interface{}) string {
return fmt.Sprintf("%T", source)
}
////////////////////////////////////////////////////////////////////////////////