-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathtailer.go
62 lines (52 loc) · 1.04 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
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
package blackbox
import (
"io"
"log"
"os"
"strings"
"time"
"github.com/nxadm/tail"
"github.com/nxadm/tail/watch"
"code.cloudfoundry.org/blackbox/syslog"
)
type Tailer struct {
Path string
Tag string
Drainer syslog.Drainer
Logger *log.Logger
}
func (tailer *Tailer) Run(signals <-chan os.Signal, ready chan<- struct{}) error {
watch.POLL_DURATION = 1 * time.Second
tailer.Logger.Printf("Starting to tail file: %s", tailer.Path)
t, err := tail.TailFile(tailer.Path, tail.Config{
Follow: true,
ReOpen: true,
Poll: true,
Location: &tail.SeekInfo{
Offset: 0,
Whence: io.SeekEnd,
},
Logger: tailer.Logger,
})
if err != nil {
return err
}
defer t.Cleanup()
close(ready)
for {
select {
case line, ok := <-t.Lines:
if !ok {
log.Println("lines flushed; exiting tailer")
return nil
}
lineTextNoCr := strings.TrimRight(line.Text, "\r")
err = tailer.Drainer.Drain(lineTextNoCr, tailer.Tag)
if err != nil {
log.Println(err.Error())
}
case <-signals:
return t.Stop()
}
}
}