golang基础-tailf日志组件使用

golang基础-tailf日志组件使用,第1张

概述git上log日志组件 https://github.com/hpcloud/tail/blob/master/tail.go 我们写个test来测试下这个组件 package mainimport ( "fmt" "github.com/hpcloud/tail" "time")func main() { filename := "E:\\develop\

git上log日志组件
https://github.com/hpcloud/tail/blob/master/tail.go

我们写个test来测试下这个组件

package mainimport (    "fmt"    "github.com/hpcloud/tail"    "time")func main() {    filename := "E:\develop\kafka\kafka_2.12-1.0.0\config\server.propertIEs"    tails,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    }    var msg *tail.line    var ok bool    for true {        msg,ok = <-tails.lines        if !ok {            fmt.Printf("tail file close reopen,filename:%s\n",tails.filename)            time.Sleep(100 * time.Millisecond)            continue        }        fmt.Println("msg:",msg)    }}

执行代码会在终端输出如下该文件的信息:

PS E:\golang\go_pro\src\safly> go run safly.gomsg: &{# licensed to the Apache Software Foundation (ASF) under one or more 2017-11-18 19:22:49.1946892 +0800 CST <nil>}msg: &{# contributor license agreements. See the NOTICE file distributed with 2017-11-18 19:22:49.1946892 +0800 CST <nil>}msg: &{# this work for additional information regarding copyright ownership. 2017-11-18 19:22:49.2086992 +0800 CST <nil>}msg: &{# The ASF licenses this file to You under the Apache license,Version 2.0 2017-11-18 19:22:49.2102004 +0800 CST <nil>}。。。。。。省略

我们看看源码tailf.go的执行流程:

func Tailfile(filename string,config Config) (*Tail,error) {    if config.ReOpen && !config.Follow {        util.Fatal("cannot set ReOpen without Follow.")    }    t := &Tail{        filename: filename,lines:    make(chan *line),Config:   config,}    // when Logger was not specifIEd in config,use default logger    if t.Logger == nil {        t.Logger = log.New(os.Stderr,"",log.LstdFlags)    }    if t.Poll {        t.watcher = watch.NewPollingfileWatcher(filename)    } else {        t.watcher = watch.NewInotifyfileWatcher(filename)    }    if t.MustExist {        var err error        t.file,err = Openfile(t.filename)        if err != nil {            return nil,err        }    }    go t.tailfileSync()    return t,nil}

在最后go t.tailfileSync()启动goroutine
在tailfileSync方法中

tail.openReader()

看看做了什么 *** 作?

func (tail *Tail) openReader() {    if tail.Maxlinesize > 0 {        // add 2 to account for newline characters        tail.reader = bufio.NewReaderSize(tail.file,tail.Maxlinesize+2)    } else {        tail.reader = bufio.NewReader(tail.file)    }}

创建reader,然后for读取line,err := tail.readline()

然后写入chan 中

// Process `line` even if err is EOF.        if err == nil {            cooloff := !tail.sendline(line)            if cooloff {                // Wait a second before seeking till the end of                // file when rate limit is reached.                msg := ("Too much log activity; waiting a second " +                    "before resuming tailing")                tail.lines <- &line{msg,time.Now(),errors.New(msg)}                select {                case <-time.After(time.Second):                case <-tail.Dying():                    return                }                if err := tail.seekEnd(); err != nil {                    tail.Kill(err)                    return                }            }}

tail.lines <- &line{msg,time.Now(),errors.New(msg)}中的lines是在Tail定义的chan

type Tail struct { filename string lines chan *line Config file *os.file reader *bufio.Reader watcher watch.fileWatcher changes *watch.fileChanges tomb.Tomb // provIDes: Done,Kill,Dying lk sync.Mutex }
总结

以上是内存溢出为你收集整理的golang基础-tailf日志组件使用全部内容,希望文章能够帮你解决golang基础-tailf日志组件使用所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1273220.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-08
下一篇 2022-06-08

发表评论

登录后才能评论

评论列表(0条)

保存