Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Watch/read a growing log file

Tags:

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

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'

like image 602
KingInk Avatar asked Aug 18 '09 13:08

KingInk


1 Answers

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/) 

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'.

like image 71
Qianjigui Avatar answered Nov 15 '22 04:11

Qianjigui