栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在Go中更新日志文件时读取它们

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在Go中更新日志文件时读取它们

我已经编写了一个Go程序包github.com/hpcloud/tail来完成此任务。

t, err := tail.TailFile("/var/log/nginx.log", tail.Config{Follow: true})for line := range t.Lines {    fmt.Println(line.Text)}

引用kostix的答案:

在现实生活中,文件可能会被截断,替换或重命名(因为logrotate之类的工具应该这样做)。

如果文件被截断,它将自动重新打开。为了支持重新打开重命名的文件(由于logrotate等),可以设置Config.ReOpen,即:

t, err := tail.TailFile("/var/log/nginx.log", tail.Config{    Follow: true,    ReOpen: true})for line := range t.Lines {    fmt.Println(line.Text)}

Config.ReOpen
类似于
tail -F
(大写F):

 -F      The -F option implies the -f option, but tail will also check to see if the file being followed has been         renamed or rotated.  The file is closed and reopened when tail detects that the filename being read from         has a new inode number.  The -F option is ignored if reading from standard input rather than a file.


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/436205.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号