I'm writing a component of a web app that displays the tail
of a log file.
My current attempt is to use web sockets to emit the changes in the file, ensure that I have fewer than n
lines, and tack it into a text area.
My current problem is how to generate 'on change'
events for the log file in Python.
I could conceivably detect changes of some_log.log
by creating a copy some_copy.log
with shutil
, detect the last time the file was changed with os.path.getmtime
, take the diff and emit the changes. This reeks of inefficiency.
Does anyone have suggestions for asynchronously detecting changes in files using Python? I'm using Python 2.7.6.
I'm going to add this for future readers, but if others want to suggest alternative ideas, I'll happily entertain them.
I found a package called tailer which is a wrapper around the unix tail program. Roughly in pythonic pseudocode, I plan to do the following in a separate thread:
for line in tailer.follow(open('some_log.log')):
emit(line)