forked from zentures/sequence
-
Notifications
You must be signed in to change notification settings - Fork 3
/
logrecord.go
136 lines (131 loc) · 3.6 KB
/
logrecord.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
package sequence
import (
"bufio"
"encoding/json"
"strings"
)
type LogRecord struct {
Service string `json:"service"`
Message string `json:"message"`
}
type LogRecordCollection struct {
Service string
Records []LogRecord
}
//This method expects records in the format {"service": "service-name", message: "log message"}
//eg {"service":"remctld","message":"error receiving initial token: unexpected end of file"} if json or for text
//service [space] message, eg: remctld error receiving initial token: unexpected end of file.
//See Examples folder for example files.
//Returns a collection of log records.
func ReadLogRecord(fname string, format string, lr []LogRecord, batchLimit int) []LogRecord {
iscan, ifile, err := OpenInputFile(fname)
defer ifile.Close()
if err != nil {
logger.HandleFatal(err.Error())
}
var r LogRecord
var count = 0
for iscan.Scan() {
message := iscan.Text()
if len(message) == 0 {
break
}
if len(message) == 0 || message[0] == '#' {
continue
}
if format == "json" {
r = LogRecord{}
_ = json.Unmarshal([]byte(message), &r)
//check for an empty service and set it to none
//TODO: Review if these should be discarded too
if r.Service == "" {
r.Service = "none"
}
} else {
//the first field is the service, delimited by a space
k := strings.Fields(message)
s := k[0]
//we need to remove the service from the remaining message
i := len(s) + 1
if i < len(message) {
m := message[i:]
r = LogRecord{Service: s, Message: m}
}
}
//check for an empty message and discard
if len(strings.TrimSpace(r.Message)) == 0 {
continue
}
lr = append(lr, r)
count++
if batchLimit != 0 && count >= batchLimit {
break
}
}
//fmt.Printf("File loaded: %d records found\n", len(lr))
return lr
}
//This method expects records in the format {"service": "service-name", message: "log message"}
//eg {"service":"remctld","message":"error receiving initial token: unexpected end of file"} if json or for text
//service [space] message, eg: remctld error receiving initial token: unexpected end of file.
//See Examples folder for example files.
//Returns a map.
func ReadLogRecordAsMap(iscan *bufio.Scanner, format string, smap map[string]LogRecordCollection, batchLimit int) (int, map[string]LogRecordCollection, bool) {
var lr LogRecordCollection
var count = 0
var exit = false
var r LogRecord
for iscan.Scan() {
message := iscan.Text()
//
if len(strings.TrimSpace(message)) == 0 {
break
}
if strings.TrimSpace(message) == "exit" {
exit = true
break
}
if message[0] == '#' {
continue
}
if format == "json" {
r = LogRecord{}
_ = json.Unmarshal([]byte(message), &r)
//check for an empty service and set it to none
//TODO: Review if these should be discarded too
if r.Service == "" {
r.Service = "none"
}
} else {
//the first field is the service, delimited by a space
k := strings.Fields(message)
s := k[0]
//we need to remove the service from the remaining message
i := len(s) + 1
if i < len(message) {
m := message[i:]
r = LogRecord{Service: s, Message: m}
} else {
r = LogRecord{Service: s, Message: ""}
}
}
//check for an empty message and discard
if len(strings.TrimSpace(r.Message)) == 0 {
continue
}
//look for the service in the map
if val, ok := smap[r.Service]; ok {
val.Records = append(val.Records, r)
smap[r.Service] = val
} else {
lr = LogRecordCollection{Service: r.Service}
lr.Records = append(lr.Records, r)
smap[r.Service] = lr
}
count++
if batchLimit != 0 && count >= batchLimit {
break
}
}
return count, smap, exit
}