Friday, November 11, 2011

Read a log file in tail mode from python

When you want to develop a log reader application, you'll need to keep reading the log. There is a Linux utility doing exactly the same. Its the tail command. tail blocks on the file till new entries are appended to it.

I needed the similar functionality in python and there was a small library providing it already. Its called The program performs tail functionality and also handles log rotation.

But in my case, the log file used to change (file name) depending on the date. filetail could not handle it as the file name has changed already. To handle this I have made following changes to the filetail code -

In nextline() method file size should be checked to be less or equal to the cursor position:

                # maybe the log got rotated out from under us?
                if stat(self.path)[ST_SIZE] <= self.pos: # modified by sarbajit
                    # file got truncated and/or re-created

In _reset() method I have added the logic to re-create the log file name from the current date value and replace it with the original file name:

    def _reset(self):
        """Internal method; reopen the internal file handle (probably
        because the log file got rotated/truncated)."""

 # modified by sarbajit
 localtime1 = localtime(time())
 year = str(localtime1[0])
 if localtime1[1] < 9:
  month = "0"+str(localtime1[1])
  month = str(localtime[1])
 if localtime1[2] > 9:
  date = str(localtime1[2])
  date = "0"+str(localtime1[2])
 logfile_name = "applog."+year+"-"+month+"-"+date+".log"
 self.path = "/var/log/" + logfile_name
 # end of modification

        self.f = open(self.path, "r")
        self.pos = self.f.tell()
        self.last_read = time()

I apologize for not following standards of coding here. It served the purpose for me.

No comments:

Post a Comment