「GoCN酷Go推荐」对标 linux tail 命令的 Golang tail 包

admin 2023年8月5日20:21:00评论25 views字数 1498阅读4分59秒阅读模式

tail 简单介绍

tail 命令用途是依照要求将指定的文件的最后部分输出到标准设备,通常是终端,通俗讲来,就是把某个档案文件的最后几行显示到终端上。

假设该档案有更新,tail 会自己主动刷新,确保你看到最新的档案内容 ,在日志收集中可以实时的监测日志的变化。

安装 tail

go get github.com/hpcloud/tail/...

快速使用 tail

使用场景:监听一个文件 info.log, 当 info.log 出现 gopherchina 字样时表明某项服务已经启动完毕

  • 首先初始化配置结构体 config

  • 调用 TailFile 函数,并传入文件路径和config,返回有个 tail 的结构体,tail 结构体的 Lines 字段封装了拿到的信息

  • 遍历 tail.Lnes 字段,取出信息(注意这里要循环取数据,因为 tail 可以实现实时监控)

package main

import (
 "fmt"
 "strings"
 "time"

 "github.com/hpcloud/tail"
)

func main() {
 fileName := "./info.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:%sn", tails.Filename)
   time.Sleep(time.Second)
   continue
  }
  if strings.Contains(line.Text, "gopherchina") {
   fmt.Println("service has been started")
   return
  }
 }
}

参考文档

  • https://github.com/hpcloud/tail
  • https://www.cnblogs.com/wind-zhou/p/12840174.html


《酷Go推荐》招募:


各位Gopher同学,最近我们社区打算推出一个类似GoCN每日新闻的新栏目《酷Go推荐》,主要是每周推荐一个库或者好的项目,然后写一点这个库使用方法或者优点之类的,这样可以真正的帮助到大家能够学习到

新的库,并且知道怎么用。


大概规则和每日新闻类似,如果报名人多的话每个人一个月轮到一次,欢迎大家报名!戳「阅读原文」,即可报名


扫码也可以加入 GoCN 的大家族哟~


「GoCN酷Go推荐」对标 linux tail 命令的 Golang tail 包


原文始发于微信公众号(GoCN):「GoCN酷Go推荐」对标 linux tail 命令的 Golang tail 包

  • 左青龙
  • 微信扫一扫
  • weinxin
  • 右白虎
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2023年8月5日20:21:00
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                   「GoCN酷Go推荐」对标 linux tail 命令的 Golang tail 包http://cn-sec.com/archives/620587.html

发表评论

匿名网友 填写信息