Watch/read a growing log file

后端 未结 7 1072
春和景丽
春和景丽 2020-12-08 00:54

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 ou

相关标签:
7条回答
  • 2020-12-08 01:34

    check out file-tail gem

    0 讨论(0)
  • 2020-12-08 01:49

    Working on idea of @Qianjigui but not using 100% CPU:

    def watch_for(file, pattern)
      # Replace -n0 with -n+1 if you want to read from the beginning of file
      f = IO.popen(%W[tail -f -n0 #{file}])
      loop do
        select([f])
        while line = f.gets
          puts "Found it! #{line}" if line =~ pattern
        end
      end
    end
    
    watch_for('g.txt', /ERROR/)
    
    0 讨论(0)
  • 2020-12-08 01:51
    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'.

    0 讨论(0)
  • 2020-12-08 01:52

    You can use Kernel#select in the following way:

    def watch_for(file,pattern)
       f = File.open(file,"r")
    
       # Since this file exists and is growing, seek to the end of the most recent entry
       f.seek(0,IO::SEEK_END)
    
       while true
          select([f])
          puts "Found it!" if f.gets =~ pattern
       end
    end
    

    Then call it like:

    watch_for("some_file", /ERROR/)
    

    I've elided all error checking and such - you will want to have that and probably some mechanism to break out of the loop. But the basic idea is there.

    0 讨论(0)
  • 2020-12-08 01:55

    Poor man's approach for quick stuff:

    1. a Ruby script that does

      ARGF.each do |line|
        ...
      
    2. Running screen with:

      tail -f file | ruby script 
      
    0 讨论(0)
  • 2020-12-08 01:57

    If you're on Linux...

    tail -f log/development.log | grep "ERROR"
    

    Unless you really wanted it to be a Ruby script for some reason.

    0 讨论(0)
提交回复
热议问题