方1:
import subprocess
if __name__ == '__main__':
log_file = '/tmp/debug.log'
cmd = 'tailf -1 {}'.format(log_file)
pp = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
while True:
line = pp.stdout.readline().strip()
row = line.decode()
print(row)
方2:
import time
if __name__ == '__main__':
file = open('/tmp/debug.log')
while True:
where = file.tell()
row = file.readline()
if not row:
time.sleep(1)
file.seek(where)
else:
print(row)
方3:
import time
def follow(file_o):
file_o.seek(0, 2)
while True:
row = file_o.readline()
if not row:
time.sleep(1)
continue
yield row
if __name__ == '__main__':
file = open('/tmp/debug.log')
log_line = follow(file)
for line in log_line:
print(line)
方4:
import pyinotify仅在linux平台,用来监测文件系统的变化,依赖于linux内核的inotify功能,inotify是一个事件驱动的通知器,其通知接口从内核空间到用户空间通过3个系统调用,pyinotify结合这些系统调用提供一个顶级的抽象和一个通用的方式来处理这些功能,pyinotify仅是对inotify的py封装
if __name__ == '__main__':
file = '/tmp/debug.log'
wm = pyinitofy.WatchManager()对象保存了需要监视的文件和目录,及监视文件和目录的哪些事件
wm.add_watch(file, pyinotify.ALL_EVENTS)
notifier = pyinotify.Notifier(wm)是pyinotify模块最重要的类,用来读取通知和处理事件,默认Notifier处理事件的方式是打印事件;Notifier类在初始化时接受多个参数,但仅WatchManager对象是必须传的参数,Notifier类根据WatchManager对象中的配置来决定如何处理事件
notifier.loop()
<Event dir=False mask=0x20 maskname=IN_OPEN name='' path=/tmp/debug.log pathname=/tmp/debug.log wd=1 >
<Event dir=False mask=0x1 maskname=IN_ACCESS name='' path=/tmp/debug.log pathname=/tmp/debug.log wd=1 >
<Event dir=False mask=0x10 maskname=IN_CLOSE_NOWRITE name='' path=/tmp/debug.log pathname=/tmp/debug.log wd=1 >
<Event dir=False mask=0x20 maskname=IN_OPEN name='' path=/tmp/debug.log pathname=/tmp/debug.log wd=1 >
<Event dir=False mask=0x2 maskname=IN_MODIFY name='' path=/tmp/debug.log pathname=/tmp/debug.log wd=1 >
<Event dir=False mask=0x8 maskname=IN_CLOSE_WRITE name='' path=/tmp/debug.log pathname=/tmp/debug.log wd=1 >
inotify提供了多种事件:
IN_ACCESS;
IN_MODIFY;
IN_ATTRIB,元数据被修改;
IN_CLOSE_WRITE,一个打开且等待写入的文件或目录被关闭;
IN_CLOSE_NOWRITE,一个以只读方式打开的文件或目录被关闭;
IN_OPEN;
IN_MOVED_FROM,被监控项目或目录中的文件被移除监控区域;
IN_MOVED_TO,文件或目录被移入监控区域;
IN_CREATE;
IN_DELETE;
IN_MOVE,文件被移动,等同于IN_CLOSE_NOWRITE;
multi_event = pyinotify.IN_OPEN | pyinotify.IN_CLOSE_WRITE # 可通过|监控多个事件
例:
import pyinotify, time, os
if __name__ == '__main__':
fn = '/tmp/debug.log'
file = open(fn, 'r')
class ProcessTransientFile(pyinotify.ProcessEvent):
def process_IN_MODIFY(self, event):
row = file.readline()
if row:
print(row)
st_results = os.stat(fn)
print('---stat', st_results)
st_size = st_results[6]
file.seek(st_size)
wm = pyinotify.WatchManager()
wm.watch_transient_file(fn, pyinotify.IN_MODIFY, ProcessTransientFile)
notifier = pyinotify.Notifier(wm)
notifier.loop()
]$ stat debug.log
‘debug.log’
Size: 60 Blocks: 8 IO Block: 4096 regular file
Device: fd00h/64768d Inode: 8477759 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2022-01-13 10:36:08.569134064 +0800
Modify: 2022-01-13 10:36:08.568134057 +0800
Change: 2022-01-13 10:36:08.568134057 +0800
Birth: -
]$ python3 test.py
---stat os.stat_result(st_mode=33188, st_ino=8477759, st_dev=64768, st_nlink=1, st_uid=0, st_gid=0, st_size=60, st_atime=1642041368, st_mtime=1642041368, st_ctime=1642041368)