观察/阅读不断增长的日志文件 [英] Watch/read a growing log file

查看:89
本文介绍了观察/阅读不断增长的日志文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个不断增长的日志文件。如何通过Ruby脚本观察和解析它?

I have a log file that is constantly growing. How can I watch and parse it via a Ruby script?

脚本将解析每个新行,因为它写入文件并在新的时候输出到屏幕上line包含字符串'ERROR'

The script will parse each new line as it is written to the file and output something to the screen when the new line contains the string 'ERROR'

推荐答案

def watch_for(file, pattern)
  f = File.open(file,"r")
  f.seek(0,IO::SEEK_END)
  while true do
    select([f])
    line = f.gets
    puts "Found it! #{line}" if line=~pattern
  end
end

watch_for("g.txt",/ERROR/)

感谢ezpz的想法,使用select方法可以得到你想要的。
select方法正在监听IO的流,读取迟到的字节。

Thanks for the ezpz's idea, using the select method you get get what you want. The select method is listening the IO's stream, read the bytes what comes 'late'.

这篇关于观察/阅读不断增长的日志文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆