By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,001 Members | 1,262 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,001 IT Pros & Developers. It's quick & easy.

Watch log

P: n/a
Hi guys,
I've an application that writes a log file.
I need to write an application with python that will read this file.

I would like wait until a new line will be write in the file.

Something like the "watch cat Log" command on Linux os.

How can I check the eof and restart the reading?

Jan 15 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Salvatore Di Fazio wrote:
I would like wait until a new line will be write in the file.

Something like the "watch cat Log" command on Linux os.
Why not read the file continuously and only do something if a new
line is complete (i. e. a newline char is detected)?
How can I check the eof and restart the reading?
Why do you want to check for an EOF character?

Regards,
Björn

--
BOFH excuse #212:

Of course it doesn't work. We've performed a software upgrade.

Jan 15 '07 #2

P: n/a
Bjoern Schliessmann ha scritto:
Why not read the file continuously and only do something if a new
line is complete (i. e. a newline char is detected)?
How can I read the file continuously?

Jan 15 '07 #3

P: n/a
Salvatore Di Fazio wrote:
Hi guys,
I've an application that writes a log file.
I need to write an application with python that will read this file.

I would like wait until a new line will be write in the file.

Something like the "watch cat Log" command on Linux os.

How can I check the eof and restart the reading?
You didn't provide much info so here goes. If you use the
logger module built into Python you can set up a logger that
uses sockets. Have your program log to a socket and have the
other program read from the socket. That way you will always
have the latest log entries in the monitor program.

If you can't control the program that writes the log file,
have your monitoring program look for change in the length
of the logfile (you can use the os.stat module for this).
When it changes, open the file, seek to the old length and
read bytes up to the new length and close the logfile.
What is read will be the new log entry (or entries).
Reset the logfile length high water mark and loop.

Hope information helps.

-Larry Bates
Jan 16 '07 #4

P: n/a
Salvatore Di Fazio wrote:
Bjoern Schliessmann ha scritto:

>Why not read the file continuously and only do something if a new
line is complete (i. e. a newline char is detected)?

How can I read the file continuously?

What you want is something like 'tail -f' in linux.
If you look into it's source code, you will find it check file periodically.
When a read() reach the end of the file, read() will return a empty string.
You can try to read data from the file periodically after a EOF.
I think it is what you want.

--
Thinker Li - th*****@branda.to th********@gmail.com
http://heaven.branda.to/~thinker/GinGin_CGI.py

Jan 16 '07 #5

P: n/a
Salvatore Di Fazio wrote:
Hi guys,
I've an application that writes a log file.
I need to write an application with python that will read this file.
You may begin from this:

$ cat showlog.py
import sys
from ScrolledText import ScrolledText

interval =50 # ms

def displayfile(f, textwidget):
textwidget.insert('end', f.readline())
textwidget.after(interval, displayfile, f, textwidget)

if __name__ == '__main__':
st = ScrolledText()
st.pack()
displayfile(sys.stdin, st)
st.mainloop()

$ python showlog.py < mylogfile.log

HTH,
Michele Simionato

Jan 16 '07 #6

P: n/a
Thinker ha scritto:
What you want is something like 'tail -f' in linux.
If you look into it's source code, you will find it check file periodically.
When a read() reach the end of the file, read() will return a empty string.
You can try to read data from the file periodically after a EOF.
I think it is what you want.
So every 2 sec:
- open the file
- read the file from the oldPosition to the EOF
- save the last position in file
- first point

mmm ok

Jan 16 '07 #7

P: n/a
Michele Simionato ha scritto:
You may begin from this:
Tnx Michele

Jan 16 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.