vincenshen

HP团队出的tail库,常用于日志收集

 

示例代码:

package main

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

func main() {
    filename := "./my.log"
    tailFile, 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
    }

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

 

分类:

技术点:

相关文章:

  • 2021-11-05
  • 2022-03-04
  • 2022-12-23
  • 2021-08-14
  • 2021-06-10
  • 2021-09-18
  • 2021-06-29
  • 2022-03-09
猜你喜欢
  • 2021-10-07
  • 2021-06-06
  • 2021-07-13
  • 2018-09-17
  • 2021-09-11
  • 2022-12-23
  • 2021-05-24
相关资源
相似解决方案