I am using the following function to follow and process a logfile line by line:
def tail(f):
f.seek(0,2)
while True:
line = f.readline()
if not line:
time.sleep(0.1)
continue
yield line
with open("/var/log/some.log") as f:
for line in tail(f):
...
When logrotate rotates the file, that function is stuck at the last line. I guess logrotate moves the file and creates a new one, leaving the program still waiting for lines from the old one.
What is a good way to handle this - reopen the file when rotation occurs?