Tailing a log file?

Evan Klitzke evan at yelp.com
Fri Jun 22 14:46:56 EDT 2007


On 6/22/07, Evan Klitzke <evan at yelp.com> wrote:
> Everyone,
>
> I'm interested in writing a python program that reads from a log file
> and then executes actions based on the lines. I effectively want to
> write a loop that does something like this:
>
> while True:
>     log_line = log_file.readline()
>     do_something(log_line)
>
> Where the readline() method blocks until a new line appears in the
> file, unlike the standard readline() method which returns an empty
> string on EOF. Does anyone have any suggestions on how to do this?
> Thanks in advance!

I checked the source code for tail and they actually poll the file by
using fstat and sleep to check for changes in the file size. This
didn't seem right so I thought about it more and realized I ought to
be using inotify. So I guess I answered my own question.

-- 
Evan Klitzke <evan at yelp.com>



More information about the Python-list mailing list