-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
98 lines (87 loc) · 2.47 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
package main
import (
"fmt"
"os"
"github.com/tom-draper/log-analyzer/pkg/analyze"
"github.com/tom-draper/log-analyzer/pkg/parse"
)
func main() {
if len(os.Args) < 2 {
fmt.Println("no log file paths provided\nprovide log file path(s) as command-line arguments\n\nexample:\n./main ./logs/postgres-main.log ./logs/postgres-1.log")
return
}
logPaths, configPath, test, printLines := getCommandLineArgs()
// Default to ./config.json
if configPath == "" {
configPath = "./config/config.json"
}
// Retrieve log line patterns from config file
config, err := parse.LoadConfig(configPath)
if err != nil {
fmt.Printf("failed to load patterns from %s\n", configPath)
return
}
// If testing config against log file(s), run test
if test {
if len(logPaths) == 1 {
parse.ParseFileTest(logPaths[0], &config)
} else {
parse.ParseFilesTest(logPaths, &config)
}
return
}
// Extract tokens from log files
var extraction []parse.Extraction
if len(logPaths) == 1 {
extraction, err = parse.ParseFile(logPaths[0], &config)
if err != nil {
fmt.Printf("unable to parse log file: %s\n", fmt.Sprint(err))
}
} else {
extraction, err = parse.ParseFiles(logPaths, &config)
if err != nil {
fmt.Printf("unable to parse log files: %s\n", fmt.Sprint(err))
}
}
if len(extraction) == 0 {
fmt.Println("no lines extracted\nensure log file path is correct")
return
} else if !tokensExtracted(extraction) {
fmt.Println("no tokens extracted\nensure patterns in `config/config.json` are correct and all tokens are named")
return
}
if printLines {
parse.DisplayLines(extraction)
}
analyze.Run(extraction, &config)
}
func tokensExtracted(extraction []parse.Extraction) bool {
for _, p := range extraction {
if len(p.Params) > 0 {
return true
}
}
return false
}
func getCommandLineArgs() (logPaths []string, configPath string, test bool, print bool) {
// Get log file paths from command-line arguments
logPaths = make([]string, 0)
for i := 1; i < len(os.Args); i++ {
arg := os.Args[i]
if arg == "-t" || arg == "--test" {
test = true
continue
} else if arg == "-p" || arg == "--print" || arg == "-d" || arg == "--display" {
print = true
continue
} else if arg == "-c" || arg == "--config" {
// Skip as path will be recorded next iteration
continue
} else if i > 1 && (os.Args[i-1] == "-c" || os.Args[i-1] == "--config") {
configPath = os.Args[i]
continue
}
logPaths = append(logPaths, arg)
}
return logPaths, configPath, test, print
}