# New content notification from a log file

**URL:** <https://www.ruby-forum.com/t/new-content-notification-from-a-log-file/121549>\
**Category:** Ruby\
**Created:** [November 28, 2007, 9:06pm UTC](https://www.ruby-forum.com/t/new-content-notification-from-a-log-file/121549 "2007-11-28T21:06:02Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![oliver](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/85f322/32.png) [@oliver](https://www.ruby-forum.com/u/oliver)\
**Post date:** [November 28, 2007, 9:06pm UTC](https://www.ruby-forum.com/t/new-content-notification-from-a-log-file/121549/1 "2007-11-28T21:06:02Z")

</div>

dear all,

I have the following situation that I don’t have a good solution:

One process periodically will append its output to a file (which is  
not under my control), I want to write a ruby problem to detect and  
report only the _new_ stuff that went into the output file … Besides  
the brute force way such as opening the file and counting the lines  
etc, is there a better solution to it?

Thanks for help

Oliver

---

<div class="post-metadata">

**Author:** ![oliver](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/85f322/32.png) [@oliver](https://www.ruby-forum.com/u/oliver)\
**Post date:** [November 28, 2007, 9:21pm UTC](https://www.ruby-forum.com/t/new-content-notification-from-a-log-file/121549/2 "2007-11-28T21:21:50Z")

</div>

On Nov 28, 2007, at 3:05 PM, Oliver wrote:

> One process periodically will append its output to a file (which is  
> not under my control), I want to write a ruby problem to detect and  
> report only the _new_ stuff that went into the output file … Besides  
> the brute force way such as opening the file and counting the lines  
> etc, is there a better solution to it?

You can use File.stat(‘filename’).size to get the number of bytes in  
the file or .mtime to find the last modification time. If the bytes  
and/or time changes you can then open the file and grab the new lines.

Gary W.

---

<div class="post-metadata">

**Author:** ![oliver](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/85f322/32.png) [@oliver](https://www.ruby-forum.com/u/oliver)\
**Post date:** [November 28, 2007, 9:33pm UTC](https://www.ruby-forum.com/t/new-content-notification-from-a-log-file/121549/3 "2007-11-28T21:33:32Z")

</div>

You can do something like this:

f = File.new(‘logfile.log’)

while true  
while(line = f.gets)  
puts line  
end  
sleep 5  
end

---

<div class="post-metadata">

**Author:** ![oliver](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/85f322/32.png) [@oliver](https://www.ruby-forum.com/u/oliver)\
**Post date:** [November 28, 2007, 10:21pm UTC](https://www.ruby-forum.com/t/new-content-notification-from-a-log-file/121549/4 "2007-11-28T22:21:02Z")

</div>

Thanks to both of you for the solution.

The double while loop seems a bit more concise, but it depends on  
File#gets method handles the changing file content correctly. I will  
give it a spin.

Best,

Oliver

---

<div class="post-metadata">

**Author:** ![oliver](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/85f322/32.png) [@oliver](https://www.ruby-forum.com/u/oliver)\
**Post date:** [November 28, 2007, 10:31pm UTC](https://www.ruby-forum.com/t/new-content-notification-from-a-log-file/121549/5 "2007-11-28T22:31:00Z")

</div>

On Nov 28, 2007 4:20 PM, Oliver [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Nov 28, 3:33 pm, Christian von Kleist [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > > > etc, is there a better solution to it?
> > 
> > > You can use File.stat(‘filename’).size to get the number of bytes in  
> > > the file or .mtime to find the last modification time. If the bytes  
> > > and/or time changes you can then open the file and grab the new lines.
> > 
> > > Gary W.

You’re welcome! That approach has worked for me many times in the  
past in many languages, including Ruby. Give it a run!
