course/kafka/tailf_demo/main.go

37 lines
834 B
Go
Raw Normal View History

2024-07-30 00:50:22 +08:00
package main
import (
"fmt"
"github.com/hpcloud/tail"
"time"
)
// tail 示例
func main() {
fileName := "./my.log"
config := tail.Config{
Location: &tail.SeekInfo{Offset: 0, Whence: 2}, //从文件的哪个地方开始读 filebeat记录了文件断点的位置
ReOpen: true, //重新打开,切分用
MustExist: false, //文件不存在不报错
Poll: true,
Follow: true, //是否跟随
}
tails, err := tail.TailFile(fileName, config)
if err != nil {
fmt.Printf("tail file error:%s\n", err)
}
var (
line *tail.Line
ok bool
)
for {
line, ok = <-tails.Lines
if !ok {
fmt.Printf("tail file closed reopen,file name:%s\n", tails.Filename)
time.Sleep(1 * time.Second)
continue
}
fmt.Println("msg:", line.Text)
}
}