go語言的地方包tail的簡單使用
參考鏈接:
tail包的作用
tail命令用途是依照要求將指定的文件的最后部分輸出到標准設備,通常是終端,通俗講來,就是把某個檔案文件的最后幾行顯示到終端上,假設該檔案有更新,tail會自己主動刷新,確保你看到最新的檔案內容 ,在日志收集中可以實時的監測日志的變化。
實戰案例
package main
import (
"fmt"
"time"
"github.com/hpcloud/tail"
)
// tailf的用法示例
func main() {
fileName := "./my.log"
config := tail.Config{
ReOpen: true, // 重新打開
Follow: true, // 是否跟隨
Location: &tail.SeekInfo{Offset: 0, Whence: 2}, // 從文件的哪個地方開始讀
MustExist: false, // 文件不存在不報錯
Poll: true,
}
tails, err := tail.TailFile(fileName, config)
if err != nil {
fmt.Println("tail file failed, err:", err)
return
}
var (
line *tail.Line
ok bool
)
for {
line, ok = <-tails.Lines//遍歷chan,讀取日志內容
if !ok {
fmt.Printf("tail file close reopen, filename:%s\n", tails.Filename)
time.Sleep(time.Second)
continue
}
fmt.Println("line:", line.Text)
}
}
簡單講解
type Tail
type Tail struct {
Filename string
Lines chan *Line
Config
tomb.Tomb // provides: Done, Kill, Dying
// contains filtered or unexported fields
}
func TailFile
func TailFile(filename string, config Config) (*Tail, error)
TailFile begins 傳入參數:日志文件的路徑和配置文件,返回一個指向Tail結構體對象的指針。
config的數據結構為:
type Config
type Config struct {
// File-specifc
Location *SeekInfo // Seek to this location before tailing
ReOpen bool // Reopen recreated files (tail -F)
MustExist bool // Fail early if the file does not exist
Poll bool // Poll for file changes instead of using inotify
Pipe bool // Is a named pipe (mkfifo)
RateLimiter *ratelimiter.LeakyBucket
// Generic IO
Follow bool // Continue looking for new lines (tail -f)
MaxLineSize int // If non-zero, split longer lines into multiple lines
// Logger, when nil, is set to tail.DefaultLogger
// To disable logging: set field to tail.DiscardingLogger
Logger logger
}
Config 用來定義文件被讀取的方式。
Tail結構體中最重要的是Lines字段,他是存儲Line指針來的一個通道。
Line的數據結構為:
type Line
type Line struct {
Text string
Time time.Time
Err error // Error from tail
}
這個結構體是用來存儲讀取的信息。
最后簡要總結一下流程:
- 首先初始化配置結構體config
- 調用TailFile函數,並傳入文件路徑和config,返回有個tail的結構體,tail結構體的Lines字段封裝了拿到的信息
- 遍歷tail.Lnes字段,取出信息(注意這里要循環的取,因為tail可以實現實時監控)
貼上實戰代碼:
package taillog
import (
"fmt"
"github.com/hpcloud/tail"
)
var (
tailObj *tail.Tail
//LogChan chan string
)
func Init (filename string)(err error){
config := tail.Config{
// File-specifc
Location: &tail.SeekInfo{Offset: 0, Whence: 2}, // 從文件那個位置開始讀
ReOpen: true, //是否重新打開
MustExist: false, // Fail early if the file does notexist
Poll: true, // Poll for file changes instead of using inotify
Follow: true, // Continue looking for new lines (tail -f)
}
tailObj,err = tail.TailFile(filename, config) //TailFile(filename, config)
if err != nil {
fmt.Println("tail file err=", err)
return
}
return
}
func ReadChan()<-chan *tail.Line{
return tailObj.Lines
}