官方地址:https://github.com/hpcloud/tail
功能:实现类似于Linux中tail -f功能
测试:
package main
import (
"fmt"
"github.com/hpcloud/tail"
"time"
)
func main() {
filename := "./app.log"
tails, err := tail.TailFile(filename, tail.Config{
ReOpen: true,
Follow: true,
// Location: &tail.SeekInfo{Offset: 0, Whence: 2},
MustExist: false,
Poll: true,
})
if err != nil {
fmt.Println("tail file err:", err)
return
}
var msg *tail.Line
var ok bool
for {
msg, ok = <-tails.Lines
if !ok {
fmt.Printf("tail file close reopen, filename:%s\n", tails.Filename)
time.Sleep(100 * time.Millisecond)
continue
}
fmt.Printf("msg(%T): %v\n", msg,msg.Text)
}
}
go mod init
go build