Python Parsing Log File for Real-Time Event Extraction

I have a process that logs messages in a file.

I want to implement another process (in Python) that analyzes these logs (since they are written to a file), filters the lines of interest to me, and then performs certain actions based on the state of the first process.

I was wondering before going further and writing something on my own if Python has a library that does something like this.

Ideas on how to implement something like this Python will also be appreciated.

Thank.

+5
source share
3 answers
Programs

C , " ". @9000 , python, -, , , .

, . , , , . :

f = open('some.log', 'r')
while True:
    line = ''
    while len(line) == 0 or line[-1] != '\n':
        tail = f.readline()
        if tail == '':
            time.sleep(0.1)          # avoid busy waiting
            # f.seek(0, io.SEEK_CUR) # appears to be unneccessary
            continue
        line += tail
    process(line)
+12

tail -f. Plain Python :

with open('/tmp/track-this') as f:
  while True:
    line = f.readline()
    if line:
      print line

, tail -f. , :

echo "more" >> /tmp/track-this
# alt-tab here to the terminal with Python and see 'more' printed
echo "even more" >> /tmp/track-this

/tmp/track-this Python.

. , /.

, ^C.

+9

Thanks to everyone for the answers. I also found this. http://www.dabeaz.com/generators/follow.py

+1
source

All Articles