"github.com/hpcloud/tail"
package logtail import ( "fmt" "github.com/hpcloud/tail" ) var ( tailObj *tail.Tail ) func Init(path string) (err error) { tailObj, err = tail.TailFile(path, tail.Config{Follow: true, ReOpen: true, MustExist: false}) if err != nil { fmt.Printf("init logtail failed,%v\n", err) return } return }
func ReadLinesChan() chan *tail.Line { return tailObj.Lines // Lines返回的是一个管道chan,管道里面存的是tail.Line每行的指针 }
line := <- ReadLinesChan() fmt.Printf("tail line:%v\n", line.Text)