Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Tailing a log file?

Reply
Thread Tools

Re: Tailing a log file?

 
 
Kenji Noguchi
Guest
Posts: n/a
 
      06-22-2007
something like this? unix tail command does more fancy stuff
like it waits for timeout, and check if the file is truncated
or depending on incoming data it sleeps seconds , etc etc.

#!/usr/bin/env python
import sys, select

while True:
ins, outs, errs = select.select([sys.stdin],[],[])
for i in ins:
print i.readline()


2007/6/22, Evan Klitzke <(E-Mail Removed)>:
> On 6/22/07, Evan Klitzke <(E-Mail Removed)> 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 <(E-Mail Removed)>
> --
> http://mail.python.org/mailman/listinfo/python-list
>

 
Reply With Quote
 
 
 
 
Falcolas
Guest
Posts: n/a
 
      06-22-2007
On Jun 22, 12:50 pm, "Kenji Noguchi" <(E-Mail Removed)> wrote:
> > 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.


I built something which worked on the "check stat and sleep" method.
Using notify or select may work, but it's not portible to Windows
systems, which may or may not be an issue for you.

#! /usr/bin/env python

import os
import sys
import time
from stat import *

class Watchfile(object):

def __init__(self, f_loc):

self.file_loc = f_loc

self.prev_lm = 0
self.last_read_pos = 0

f = open(self.file_loc, "r")
for l in f:
pass
self.last_read_pos = f.tell()
f.close()

self.prev_lm = os.stat(self.file_loc)[ST_MTIME]

def changed(self):

lm_time = os.stat(self.file_loc)[ST_MTIME]

if lm_time > self.prev_lm:
return True
else:
return False

def get_since_last_read(self):

f = open(self.file_loc, "r")

f.seek(self.last_read_pos)
lines = f.readlines()
self.last_read_pos = f.tell()

f.close

self.prev_lm = os.stat(self.file_loc)[ST_MTIME]
return lines

if __name__ == "__main__":
x_file = Watchfile("<file path>")
y_file = Watchfile("<file path>")

while True:

if x_file.changed():

lines = x_file.get_since_last_read()

# do something

if y_file.changed():

lines = y_file.get_since_last_read()

# do something else

try:
time.sleep(5)
except KeyboardInterrupt:
break

 
Reply With Quote
 
 
 
 
sjdevnull@yahoo.com
Guest
Posts: n/a
 
      06-22-2007
On Jun 22, 2:50 pm, "Kenji Noguchi" <(E-Mail Removed)> wrote:
> something like this? unix tail command does more fancy stuff
> like it waits for timeout, and check if the file is truncated
> or depending on incoming data it sleeps seconds , etc etc.
>
> #!/usr/bin/env python
> import sys, select
>
> while True:
> ins, outs, errs = select.select([sys.stdin],[],[])
> for i in ins:
> print i.readline()


select doesn't work on regular files (only pipes/sockets/etc).
inotify is platform-specific; the portable way to do this is to poll,
like tail does. If you know you're on a platform that has inotify or
something like it, it's probably the more efficient (and lower
latency) route to go.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Tailing a log file? Evan Klitzke Python 0 06-22-2007 06:20 PM
Removing the leading and tailing blanks jalkadir C++ 6 10-15-2005 03:34 AM
tailing a log file using c ... Tony Stock C Programming 10 11-21-2003 04:38 PM
Need help on the Permissions needed to log to Event Log from ASP.NET? Henrik_the_boss ASP .Net 0 11-05-2003 10:14 AM
win32: 'Tailing' the NT event log using pipes? lebo Python 0 09-18-2003 12:14 AM



Advertisments