-
Notifications
You must be signed in to change notification settings - Fork 12
/
tailer.go
35 lines (28 loc) · 1.07 KB
/
tailer.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
package main
import (
"fmt"
"github.com/cskr/pubsub"
"github.com/jdrews/go-tailer/fswatcher"
"github.com/jdrews/go-tailer/glob"
"time"
)
// Follow begins a tailer for the specified logFilePath and publishes log lines to the given pubSub message broker
// When Follow picks up a log line, it also runs the line through regex via func Colorize
// to determine if it matches a color pattern
func Follow(logFilePath string, pubSub *pubsub.PubSub, patterns []CompiledRegexColors, polling bool, pollingRateMS int) {
var tailer fswatcher.FileTailer
parsedGlob, err := glob.Parse(logFilePath)
if err != nil {
panic(fmt.Sprintf("%q: failed to parse glob: %q", parsedGlob, err))
}
if polling {
tailer, err = fswatcher.RunPollingFileTailer([]glob.Glob{parsedGlob}, false, true, time.Duration(pollingRateMS)*time.Millisecond, logger)
} else {
tailer, err = fswatcher.RunFileTailer([]glob.Glob{parsedGlob}, false, true, logger)
}
for line := range tailer.Lines() {
logger.Debug(line.Line)
logMessage := Colorize(line.Line, line.File, patterns)
pubSub.Pub(logMessage, "lines")
}
}