log-agent/taillog/taillog.go

32 lines
725 B
Go
Raw Normal View History

2024-08-01 00:45:07 +08:00
package taillog
import (
"fmt"
"github.com/hpcloud/tail"
)
2024-08-29 00:42:11 +08:00
//专门从日志文件收集日志的模块
2024-08-01 00:45:07 +08:00
var (
tailChan *tail.Tail
)
func Init(fileName string) (err error) {
config := tail.Config{
Location: &tail.SeekInfo{Offset: 0, Whence: 2}, //从文件的哪个地方开始读 filebeat记录了文件断点的位置
ReOpen: true, //重新打开,切分用
MustExist: false, //文件不存在不报错
Poll: true,
Follow: true, //是否跟随
}
tailChan, err = tail.TailFile(fileName, config)
if err != nil {
fmt.Printf("tail file error:%s\n", err)
}
return
}
2024-08-29 00:42:11 +08:00
func ReadChan() <-chan *tail.Line {
return tailChan.Lines
}