-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
161 lines (140 loc) · 4.39 KB
/
main.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
package main
import (
"encoding/json"
"flag"
"fmt"
"io"
"os"
"regexp"
"strings"
"github.com/hpcloud/tail"
)
func main() {
// Flags
filename := flag.String("f", "postgresql-example.csv", "name of CSV log file")
flag.Parse()
entryParts := make([]string, 0)
var timestampRegexp *regexp.Regexp
timestampRegexp, _ = regexp.Compile("^\\d{4}-\\d{2}-\\d{2}\\ \\d{2}:\\d{2}:\\d{2}\\.\\d{3}")
csvlog, err := tail.TailFile(*filename, tail.Config{
Follow: true,
Location: &tail.SeekInfo{
Offset: 0,
Whence: os.SEEK_END,
},
})
if err != nil {
panic(err.Error())
}
for tailLine := range csvlog.Lines {
line := tailLine.Text
if err != nil {
if err == io.EOF {
break
} else {
panic(err.Error())
}
}
if err != nil {
panic(err.Error())
}
if i := timestampRegexp.FindIndex([]byte(line)); i != nil {
// Start of log entry
values := splitLogEntry(entryParts)
l := len(values)
if l == 0 {
// Skip first or empty message
} else if l == 22 {
fmt.Println(jsonizeFromMap(convertToMap(values)))
} else {
// Brocken entry
fmt.Println(jsonizeFromMap(convertBrockenEntryToMap(values)))
}
entryParts = make([]string, 0)
entryParts = append(entryParts, line)
} else {
// Part of previous log entry
entryParts = append(entryParts, line)
}
}
}
func splitLogEntry(parts []string) []string {
values := make([]string, 0)
// Parse parts
remainPart := ""
for _, p := range parts {
isOpenedDoubleQuotes := false
indexPointer := 0
p = remainPart + p
for j, c := range p {
if c == '"' {
isOpenedDoubleQuotes = !isOpenedDoubleQuotes
} else {
if !isOpenedDoubleQuotes && c == ',' && j != 0 {
if p[indexPointer] == ',' {
values = append(values, repairDoubleQuotes(p[indexPointer+1:j]))
} else {
values = append(values, repairDoubleQuotes(p[indexPointer:j]))
}
indexPointer = j
}
}
}
remainPart = p[indexPointer:]
}
return values
}
func repairDoubleQuotes(str string) string {
l := len(str)
// Remove start/end
if l < 2 {
return str
} else if str == "\"\"" {
return ""
} else {
if str[0] == '"' && str[l-1] == '"' {
return strings.Replace(str[1:l-1], "\"\"", "\"", -1) // doublequotes deduplication
}
return strings.Replace(str, "\"\"", "\"", -1) // doublequotes deduplication
}
}
func convertToMap(values []string) map[string]string {
retmap := make(map[string]string)
// This field just crush log message
// retmap["time"] = values[0] // log_time timestamp(3) with time zone,
retmap["user_name"] = values[1] // user_name text,
retmap["database_name"] = values[2] // database_name text,
retmap["process_id"] = values[3] // process_id integer,
retmap["connection_from"] = values[4] // connection_from text,
retmap["session_id"] = values[5] // session_id text,
retmap["session_line_num"] = values[6] // session_line_num bigint,
retmap["command_tag"] = values[7] // command_tag text,
retmap["session_start_time"] = values[8] // session_start_time timestamp with time zone,
retmap["virtual_transaction_id"] = values[9] // virtual_transaction_id text,
retmap["transaction_id"] = values[10] // transaction_id bigint,
retmap["error_severity"] = values[11] // error_severity text,
retmap["sql_state_code"] = values[12] // sql_state_code text,
retmap["log_message"] = values[13] // detail text,
retmap["hint"] = values[14] // hint text,
retmap["internal_query"] = values[15] // internal_query text,
retmap["internal_query_pos"] = values[16] // internal_query_pos integer,
retmap["context"] = values[17] // context text,
retmap["query"] = values[18] // query text,
retmap["query_pos"] = values[19] // query_pos integer,
retmap["location"] = values[20] // location text,
// retmap["application_name"] = values[21] // application_name text, // can case errors in some elasticsearch configurations
return retmap
}
func convertBrockenEntryToMap(parts []string) map[string]string {
retmap := make(map[string]string)
retmap["error"] = "broken log entry"
retmap["log_entry"] = strings.Join(parts, "")
return retmap
}
func jsonizeFromMap(inputmap map[string]string) string {
if bytebuf, err := json.Marshal(inputmap); err != nil {
panic(err.Error())
} else {
return string(bytebuf)
}
}