-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
57 lines (51 loc) · 1.2 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
package main
import (
"bufio"
"encoding/json"
"fmt"
"os"
"regexp"
"github.com/fatih/color"
)
type StackDriverJson struct {
message string
cpf string
module string
severity string
}
func main() {
//https://github.com/buger/jsonparser
input := bufio.NewScanner(os.Stdin)
for input.Scan() {
text := input.Text()
r := regexp.MustCompile(`(?P<timestamp>\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}).\d+Z\s(?P<regularexpression>{.*}$)`)
match := r.FindStringSubmatch(text)
if len(match) > 2 {
timestamp := match[1]
jsonString := match[2]
var jsonObject map[string]interface{}
err := json.Unmarshal([]byte(jsonString), &jsonObject)
if err != nil {
fmt.Println(text)
} else {
cpf := jsonObject["cpf"]
module := jsonObject["module"]
if cpf == nil {
cpf = "None"
}
logLine := fmt.Sprintf("%s", jsonObject["message"])
//print timestamp without newline
fmt.Printf("\033[1;90m%s [%s] [%s] \033[0m", timestamp, cpf, module)
if jsonObject["severity"] == "INFO" {
color.White(logLine)
} else if jsonObject["severity"] == "ERROR" {
color.Red(logLine)
} else {
color.Yellow(logLine)
}
}
} else {
fmt.Println(text)
}
}
}