golang基础-tailf日志组件使用

前端之家收集整理的这篇文章主要介绍了golang基础-tailf日志组件使用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

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

  1. package main
  2.  
  3. import (
  4. "fmt"
  5. "github.com/hpcloud/tail"
  6. "time"
  7. )
  8. func main() {
  9. filename := "E:\\develop\\kafka\\kafka_2.12-1.0.0\\config\\server.properties"
  10. tails,err := tail.TailFile(filename,tail.Config{
  11. ReOpen: true,Follow: true,// Location: &tail.SeekInfo{Offset: 0,Whence: 2},
  12. MustExist: false,Poll: true,})
  13. if err != nil {
  14. fmt.Println("tail file err:",err)
  15. return
  16. }
  17. var msg *tail.Line
  18. var ok bool
  19. for true {
  20. msg,ok = <-tails.Lines
  21. if !ok {
  22. fmt.Printf("tail file close reopen,filename:%s\n",tails.Filename)
  23. time.Sleep(100 * time.Millisecond)
  24. continue
  25. }
  26. fmt.Println("msg:",msg)
  27. }
  28. }

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

  1. PS E:\golang\go_pro\src\safly> go run safly.go
  2. msg: &{# Licensed to the Apache Software Foundation (ASF) under one or more 2017-11-18 19:22:49.194689
  3. 2 +0800 CST <nil>}
  4. msg: &{# contributor license agreements. See the NOTICE file distributed with 2017-11-18 19:22:49.194
  5. 6892 +0800 CST <nil>}
  6. msg: &{# this work for additional information regarding copyright ownership. 2017-11-18 19:22:49.20869
  7. 92 +0800 CST <nil>}
  8. msg: &{# The ASF licenses this file to You under the Apache License,Version 2.0 2017-11-18 19:22:49.2
  9. 102004 +0800 CST <nil>}
  10. 。。。。。。省略

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

  1. func TailFile(filename string,config Config) (*Tail,error) {
  2. if config.ReOpen && !config.Follow {
  3. util.Fatal("cannot set ReOpen without Follow.")
  4. }
  5.  
  6. t := &Tail{
  7. Filename: filename,Lines: make(chan *Line),Config: config,}
  8.  
  9. // when Logger was not specified in config,use default logger
  10. if t.Logger == nil {
  11. t.Logger = log.New(os.Stderr,"",log.LstdFlags)
  12. }
  13.  
  14. if t.Poll {
  15. t.watcher = watch.NewPollingFileWatcher(filename)
  16. } else {
  17. t.watcher = watch.NewInotifyFileWatcher(filename)
  18. }
  19.  
  20. if t.MustExist {
  21. var err error
  22. t.file,err = OpenFile(t.Filename)
  23. if err != nil {
  24. return nil,err
  25. }
  26. }
  27.  
  28. go t.tailFileSync()
  29.  
  30. return t,nil
  31. }

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

  1. tail.openReader()

看看做了什么操作?

  1. func (tail *Tail) openReader() {
  2. if tail.MaxLineSize > 0 {
  3. // add 2 to account for newline characters
  4. tail.reader = bufio.NewReaderSize(tail.file,tail.MaxLineSize+2)
  5. } else {
  6. tail.reader = bufio.NewReader(tail.file)
  7. }
  8. }

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

然后写入chan 中

  1. // Process `line` even if err is EOF.
  2. if err == nil {
  3. cooloff := !tail.sendLine(line)
  4. if cooloff {
  5. // Wait a second before seeking till the end of
  6. // file when rate limit is reached.
  7. msg := ("Too much log activity; waiting a second " +
  8. "before resuming tailing")
  9. tail.Lines <- &Line{msg,time.Now(),errors.New(msg)}
  10. select {
  11. case <-time.After(time.Second):
  12. case <-tail.Dying():
  13. return
  14. }
  15. if err := tail.seekEnd(); err != nil {
  16. tail.Kill(err)
  17. return
  18. }
  19. }
  20. }

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

  1. 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 }

猜你在找的Go相关文章