forked from jwilder/dockerize
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtail.go
More file actions
39 lines (35 loc) · 670 Bytes
/
tail.go
File metadata and controls
39 lines (35 loc) · 670 Bytes
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
package main
import (
"fmt"
"log"
"os"
"github.com/hpcloud/tail"
"golang.org/x/net/context"
)
func tailFile(ctx context.Context, file string, poll bool, dest *os.File) {
defer wg.Done()
t, err := tail.TailFile(file, tail.Config{
Follow: true,
ReOpen: true,
Poll: poll,
Logger: tail.DiscardingLogger,
})
if err != nil {
log.Fatalf("unable to tail %s: %s", "foo", err)
}
// main loop
for {
select {
// if the channel is done, then exit the loop
case <-ctx.Done():
t.Stop()
t.Cleanup()
return
// get the next log line and echo it out
case line := <-t.Lines:
if line != nil {
fmt.Fprintln(dest, line.Text)
}
}
}
}