hpcloud / tail

Go package for reading from continously updated files (tail -f)
MIT License
2.7k stars 502 forks source link

tail.Config ReOpen abnormal #176

Closed GumpSun closed 2 years ago

GumpSun commented 3 years ago
seek := &tail.SeekInfo{}
seek.Offset = 0
seek.Whence = os.SEEK_END

config := tail.Config{}
config.Follow = true
//config.ReOpen = true
config.Location = seek

tails, err := tail.TailFile(filename, config)

When I stop using ReOpen,I got as below:

Stopping tail as file no longer exists: my.log

GumpSun commented 3 years ago

What's wrong with my code???

func main() { filename := "my.log"

seek := &tail.SeekInfo{}
seek.Offset = 0
seek.Whence = os.SEEK_END

config := tail.Config{}
config.Follow = true
//config.ReOpen = true
config.Location = seek

tails, err := tail.TailFile(filename, config)
if err != nil {
    fmt.Println("tail file err:", err)
    return
}

var msg *tail.Line
var ok bool
for true {
    msg, ok = <-tails.Lines
    if !ok {
        fmt.Printf("tail file close reopen, filename:%s\n", tails.Filename)
        time.Sleep(100 * time.Millisecond)
        continue
    }
    fmt.Println("msg:", msg)
}

}