Python - 尾随日志文件,可由另一个进程

时间:2018-05-11 20:03:00

标签: python

我有一个将数据记录到日志文件(data.log)的应用程序。此文件的上限为2 MB。达到上限后,它将重命名为备份文件(data.log.bak)并创建一个具有相同名称(data.log)的新日志文件并开始在那里进行日志记录。如果达到新文件的上限,它会再次将其保存到data.log.bak并删除旧的日志文件。

我需要编写一个python2.7脚本,它在日志中查找特定的字符串。它应该与主应用程序并行执行以考虑已删除的bak文件。该脚本应该适用于PC和Mac。

如何处理读取重命名/删除的文件。 我稍微使用了这段代码:https://stackoverflow.com/a/39213830/769078

def open_file(self):
    if sys.platform == 'win32':
        # get an handle using win32 API, specifying the SHARED access!
        handle = win32file.CreateFile(self.filename,
                                      win32file.GENERIC_READ,
                                      win32file.FILE_SHARE_DELETE |
                                      win32file.FILE_SHARE_READ |
                                      win32file.FILE_SHARE_WRITE,
                                      None,
                                      win32file.OPEN_EXISTING,
                                      0,
                                      None)
        # detach the handle
        detached_handle = handle.Detach()
        # get a file descriptor associated to the handle\
        file_descriptor = msvcrt.open_osfhandle(detached_handle, os.O_RDONLY)
        # open the file descriptor
        f = os.fdopen(file_descriptor)
    else:
        f = open(self.filename, 'r')

    return f


def follow(self):
    if not os.path.isfile(self.filename):
        return False

    current = self.open_file()
    try:
        ino = os.fstat(current.fileno()).st_ino
        while True:
            line = current.readline().strip()
            where = current.tell()
            print '[log]', line
            if line:  # Read a line
                if 'error' in line:  # If error found in line
                    self.found = True
                    print '[Debug] Found exception'
                    break
            elif self.stop:  # no new line and no new file. Stop reading if not stop
                print '[Debug] Stopping'
                break
            elif os.stat(self.filename).st_ino != ino:  # Reached end of file. Check if new file exists
                print '[Debug] Detected new file'
                new = self.open_file()
                current.close()
                current = new
                ino = os.fstat(current.fileno()).st_ino
            else:  # no new line and no new file and not stop. Sleep for a second
                print '[Debug] sleeping'
                current.seek(where)
                time.sleep(2)
    finally:
        # close the file
        current.close()

我使用线程与主应用程序并行运行脚本。我在这里跳过了这段代码。

*更新*

使用ino并不适合我。我可能犯了一个错误。相反,我依赖于修改时间。

def follow(self):
    if not os.path.isfile(self.filename):
        return False

    current = self.open_file()
    bak_modified_time = None
    try:
        while True:
            line = current.readline().strip()
            where = current.tell()
            if line:  # Read a line
                if 'error' in line:  # If error found in line
                    self.found = True
                    print '[Debug] Found exception'
                    break
            elif self.stop:  # no new line and no new file. Stop reading if not stop
                print '[Debug] Stopping'
                break
            elif ((not bak_modified_time and os.path.exists(self.bak_filename)) or
                  (bak_modified_time and os.path.exists(self.bak_filename) and
                   os.stat(self.bak_filename).st_mtime != bak_modified_time))
                print '[Debug] Detected new file'
                new = self.open_file()
                current.close()
                current = new
                bak_modified_time = os.stat(self.bak_filename).st_mtime
            else:  # no new line and no new file and not stop. Sleep for a second
                print '[Debug] sleeping'
                current.seek(where)
                time.sleep(2)
    finally:
        # close the file
        current.close()

0 个答案:

没有答案