我想在我的网站上添加一个日志查看器选项卡。该选项卡应该打印整个日志文件,然后仅打印新行(例如 Linux 中的 tail -F
命令)。客户端使用 HTML 和 Javascript,服务器端使用 Python。
这是我的 tail Python 函数(我在网上找到的):
@cherrypy.expose
def tail(self):
filename = '/opt/abc/logs/myLogFile.log'
f = subprocess.Popen(['tail','-F',filename],\
stdout=subprocess.PIPE,stderr=subprocess.PIPE)
p = select.poll()
p.register(f.stdout)
while True:
if p.poll(1):
print f.stdout.readline()
time.sleep(1)
这段代码确实打印了整个日志文件。但是,每次我向文件添加新行时,文件都会从头开始打印,而不是打印新行。
有什么解决办法吗?我是 Python 的新手,所以我将不胜感激任何帮助。
最佳答案
查看 pytailer
https://github.com/six8/pytailer
特别是 follow
命令:
# Follow the file as it grows
for line in tailer.follow(open('/opt/abc/logs/myLogFile.log')):
print line
关于python - 跟踪日志文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32375374/