1. package main
    2. import (
    3. "fmt"
    4. "time"
    5. "github.com/hpcloud/tail"
    6. )
    7. //tailf
    8. //下载 go get github.com/hpcloud/tail 日志采集
    9. //go get github.com/Shopify/sarama 压缩算法
    10. // tailf的用法示例
    11. func main() {
    12. fileName := "./my111.log"
    13. config := tail.Config{
    14. ReOpen: true, // 重新打开
    15. Follow: true, // 是否跟随
    16. Location: &tail.SeekInfo{Offset: 0, Whence: 2}, // 从文件的哪个地方开始读
    17. MustExist: false, // 文件不存在不报错
    18. Poll: true,
    19. }
    20. tails, err := tail.TailFile(fileName, config)
    21. if err != nil {
    22. fmt.Println("tail file failed, err:", err)
    23. return
    24. }
    25. var (
    26. line *tail.Line
    27. ok bool
    28. )
    29. for {
    30. line, ok = <-tails.Lines
    31. if !ok {
    32. fmt.Printf("tail file close reopen, filename:%s\n", tails.Filename)
    33. time.Sleep(time.Second)
    34. continue
    35. }
    36. fmt.Println("line:", line.Text)
    37. }
    38. }