52.tailf日志组件

https://github.com/hpcloud/tail

package main

import (
    "fmt"
    "github.com/hpcloud/tail"
    "time"
)

func main() {
    logfile := "/Users/zhoushuai/go/src/www.jhoushuai.com/studyProject/day13/GoTailF/xx.log"
    config := tail.Config{
        ReOpen:    true,                                 // 重新打开
        Follow:    true,                                 // 是否跟随
        Location:  &tail.SeekInfo{Offset: 0, Whence: 2}, // 从文件的那个位置开始读
        MustExist: false,                                // 文件不存在不报错
        Poll:      true,
    }
    contents, err := tail.TailFile(logfile, config)
    if err != nil {
        fmt.Println(err)
        return
    }

    //for line := range contents.Lines {
    //  fmt.Println(line.Text)
    //}
    var (
        line *tail.Line
        ok   bool
    )
    for {
        line, ok = <-contents.Lines
        if !ok {
            fmt.Printf("tail file close reopen ,filename %s", contents.Filename)
            time.Sleep(time.Second)
            continue
        }
        fmt.Println(line.Text)
    }
}

你可能感兴趣的:(52.tailf日志组件)