-
Notifications
You must be signed in to change notification settings - Fork 0
/
byte-logs.go
64 lines (55 loc) · 1.33 KB
/
byte-logs.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
package log
import (
"bytes"
"encoding/json"
"strings"
)
type ByteLogs struct {
Log *bytes.Buffer
Parsed []map[string]interface{}
}
func (b *ByteLogs) Parse(log *bytes.Buffer) {
// the buffer will contain the latest log since it is a pointer, but this function will need to be called
// to have the latest values in ParsedLogs for use with LogInLogs
if log != nil {
b.Log = log
}
if b.Log == nil || b.Log.Len() == 0 {
return
}
logs := strings.Split(b.Log.String(), "\n")
objLogs := make([]map[string]interface{}, 0)
for _, v := range logs {
var obj map[string]interface{}
err := json.Unmarshal([]byte(v), &obj)
if err == nil {
objLogs = append(objLogs, obj)
}
}
b.Parsed = objLogs
}
func (b *ByteLogs) LogInLogs(key string, value interface{}) bool {
for _, log := range b.Parsed {
entry, ok := log[key]
if ok && entry == value {
return true
}
}
return false
}
func (b *ByteLogs) CheckForLogEndMessage(customEnd ...string) bool {
haveStop := false
if len(customEnd) > 0 {
for _, msg := range customEnd {
haveStop = b.LogInLogs("msg", msg)
if haveStop {
break
}
}
} else {
goodStop := b.LogInLogs("msg", "task-consumer: successfully processed message")
badStop := b.LogInLogs("msg", "task-consumer: failed to process message")
haveStop = goodStop || badStop
}
return haveStop
}