Watch log

Discussion in 'Python' started by Salvatore Di Fazio, Jan 15, 2007.

  1. 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?
     
    Salvatore Di Fazio, Jan 15, 2007
    #1
    1. Advertising

  2. 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.
     
    Bjoern Schliessmann, Jan 15, 2007
    #2
    1. Advertising

  3. 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?
     
    Salvatore Di Fazio, Jan 15, 2007
    #3
  4. Salvatore Di Fazio

    Larry Bates Guest

    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
     
    Larry Bates, Jan 16, 2007
    #4
  5. Salvatore Di Fazio

    Thinker Guest

    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 -
    http://heaven.branda.to/~thinker/GinGin_CGI.py
     
    Thinker, Jan 16, 2007
    #5
  6. 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
     
    Michele Simionato, Jan 16, 2007
    #6
  7. 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
     
    Salvatore Di Fazio, Jan 16, 2007
    #7
  8. Michele Simionato ha scritto:

    > You may begin from this:


    Tnx Michele
     
    Salvatore Di Fazio, Jan 16, 2007
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Henrik_the_boss
    Replies:
    0
    Views:
    2,665
    Henrik_the_boss
    Nov 5, 2003
  2. Amratash
    Replies:
    0
    Views:
    532
    Amratash
    Apr 13, 2004
  3. =?Utf-8?B?VG9tIFdpbmdlcnQ=?=

    My.Log.Writeexception not writing to Application Event Log.

    =?Utf-8?B?VG9tIFdpbmdlcnQ=?=, Jan 20, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    2,388
    =?Utf-8?B?VG9tIFdpbmdlcnQ=?=
    Jan 20, 2006
  4. unomystEz
    Replies:
    0
    Views:
    560
    unomystEz
    Nov 19, 2006
  5. vj
    Replies:
    0
    Views:
    682
Loading...

Share This Page