-
Notifications
You must be signed in to change notification settings - Fork 0
/
springparse.go
92 lines (83 loc) · 1.77 KB
/
springparse.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
package springparse
import (
"bytes"
"github.com/hunkeelin/go-tail/follower"
log "github.com/sirupsen/logrus"
"io"
"os"
"path/filepath"
)
// Client the struct that runs the program
type Client struct {
tailedFiles map[string]int
}
// New creates a client struct with map initialized
func New() *Client {
m := make(map[string]int)
return &Client{
tailedFiles: m,
}
}
// Runner Runner spawn by go routine
type Runner struct {
Buffer *elasticOut
BufferId string
}
// SpringParse This is the main program
func (r *Client) SpringParse() {
logFiles, err := r.listDirectory()
if err != nil {
log.Error("Unable to list directory: " + err.Error())
return
}
for _, fi := range logFiles {
result := r.shouldWatch(shouldWatchInput{
logFile: fi,
})
_, ok := r.tailedFiles[fi]
if result.watch && !ok {
log.Info("Tailing " + fi)
r.tailedFiles[fi] = 0
newRunner := Runner{}
go newRunner.tailFile(fi)
}
}
return
}
func (r *Runner) tailFile(fileName string) {
t, err := follower.New(fileName, follower.Config{
Whence: io.SeekEnd,
Offset: 0,
Reopen: true,
})
if err != nil {
log.Error("Unable to create follower: " + err.Error())
return
}
for line := range t.Lines() {
err := r.sendElasticSearch(sendElasticSearchInput{
rawLog: bytes.Replace(line.Bytes(), []byte("\n"), []byte(""), -1),
fileName: fileName,
})
if err != nil {
putFailed.Inc()
log.Error(err.Error())
continue
}
}
if t.Err() != nil {
log.Error("Follower error: " + t.Err().Error())
return
}
}
func (r *Client) listDirectory() ([]string, error) {
var files []string
err := filepath.Walk(logDirectory, func(path string, info os.FileInfo, err error) error {
files = append(files, path)
return nil
})
if err != nil {
return files, err
}
return files, nil
}