0

Question says all about my requirement, but I'll try to explain in detail.

I have a log file, say development.log, and like a normal log file as requests are made, this file grows. I want to watch for this file and then if a new line(request) is added to this log file, I want to do something with this new line(say just print it). So everytime a new line is added, it should print in my rails console.

How can I achieve this in Ruby Script?

EDIT: Adding more info

Ex:

  1. If I have a initial log file:

    # development.log
    GET abcd.com
    POST efgh.com
    
  2. Now, I will run my ruby script, which will watch this log file for changes.

  3. After 5 seconds the the log file becomes:

    # development.log
    GET abcd.com
    POST efgh.com
    GET newdomain.com
    POST newdomain.com
    
  4. Now since the log file changed, My ruby console must print these new lines:

    # ruby output
    GET newdomain.com
    POST newdomain.com
    
  • I've checked a bit of `guard/listen` lib and a couple of others, but couldn't build a workaround around my requirement. –  Sep 28 '19 at 03:49
  • Is altering the code that writes to the log file an option? If not, is it important to perform the operation (e.g., printing to the console) each time a line is added to the file, or can the operation be performed periodically on groups of added lines? Some gems, such as [Logger](https://github.com/ruby/logger), may be of use. – Cary Swoveland Sep 28 '19 at 04:09
  • Anything is fine - real time or periodically. I want to get the new lines added to the log file in my ruby script, so that I can perform an operation(e.g., printing that new line(s) to the console) –  Sep 28 '19 at 05:13
  • Hey @CarySwoveland I've updated my question for more info –  Sep 28 '19 at 05:18
  • Could you post with a method such as `def log(fname, str); puts str; open(fname, 'a') { |f| f.puts str }` (where `'a'` opens the file to *append* text)? – Cary Swoveland Sep 28 '19 at 06:09
  • @CarySwoveland the new line(s) in my `development.log` comes from the web server. I don't want a ruby script that appends the new lines in a file. What I want is, when a web server logs a request in `development.log`, my ruby script must be watching/listening to this log file and then perform some operation. It is similar like `tail -f development.log`, but with `tail` I cannot perform any operations. –  Sep 28 '19 at 07:03
  • I created various methods around various libs, nothing seems to work. So I'm looking for complete solution here –  Sep 28 '19 at 07:04
  • 1
    I think this [gem:File-Tail](https://github.com/flori/file-tail) should do the job for you. It allows you to invoke a block per line of added log. Also, I think this question has been asked a lot before. Is your use case quite different from ones like these: https://stackoverflow.com/questions/1293695/watch-read-a-growing-log-file – erosenin Sep 28 '19 at 08:09
  • @erosenin I came across that answer, but it only works againt `ERROR` pattern, I want to get all the requests(or lines). Do you have any workaround for that? –  Sep 28 '19 at 15:54
  • @erosenin , I got a workaround with `file-tail` gem, but it's not updated in 2.5 years. So not sure to use that in production. Is there a pure vanilla way? –  Sep 28 '19 at 16:14
  • 1
    @allenbrkn All you have to do is either edit the function to remove this check `puts "Found it!" if f.gets =~ pattern` and just do `puts f.gets`. OR pass the second argument to match everything `watch_for("log/development.log", /.*/)` – erosenin Sep 30 '19 at 06:13

0 Answers0