From 369024f04e8582a2aee503416a670418e9d57e50 Mon Sep 17 00:00:00 2001 From: David Sommerseth Date: Thu, 15 Nov 2012 02:27:24 +0100 Subject: Added a simple logfile reopen mechanism If logrotate has been run inbetween since last time the log file was checked, the opened fd will not point at the new file. In this case reopen the log file and process all new events in this new file. Signed-off-by: David Sommerseth --- LogActio/__init__.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/LogActio/__init__.py b/LogActio/__init__.py index 6456b91..5a7c167 100644 --- a/LogActio/__init__.py +++ b/LogActio/__init__.py @@ -74,6 +74,7 @@ class WatcherThread(threading.Thread): self.start() def run(self): + fp = None # This is started by threading.Thread try: fp = fp = open(self.__logfile, "r") @@ -87,8 +88,21 @@ class WatcherThread(threading.Thread): where = fp.tell() line = fp.readline() if len(line) == 0: + # Before sleeping, grab a copy of the file size. + # If it has become truncated, we need to reopen the file + filesize_before = os.stat(self.__logfile).st_size time.sleep(self.__polltime) - fp.seek(where) + filesize_after = os.stat(self.__logfile).st_size + if filesize_after < filesize_before: + # Reopen is needed. + fp.close() + time.sleep(1) # Just in case + fp = open(self.__logfile) + # We will not go to end of file, so + # we catch all changes in the log file + # since the truncation point + else: + fp.seek(where) continue now = int(time.time()) -- cgit