如何在 Python 中跟踪日志文件? [英] How can I tail a log file in Python?

查看:85
本文介绍了如何在 Python 中跟踪日志文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让 tail -F 的输出或类似的东西在 Python 中可用,而不会阻塞或锁定.我在这里找到了一些非常旧的代码,但我认为必须有更好的方式或图书馆现在做同样的事情.有人知道吗?

I'd like to make the output of tail -F or something similar available to me in Python without blocking or locking. I've found some really old code to do that here, but I'm thinking there must be a better way or a library to do the same thing by now. Anyone know of one?

理想情况下,我有类似 tail.getNewData() 的东西,每次我想要更多数据时都可以调用它.

Ideally, I'd have something like tail.getNewData() that I could call every time I wanted more data.

推荐答案

所以,这已经很晚了,但我又遇到了同样的问题,现在有了更好的解决方案.只需使用 pygtail:

So, this is coming quite late, but I ran into the same problem again, and there's a much better solution now. Just use pygtail:

Pygtail 读取尚未读取的日志文件行.甚至会处理已轮换的日志文件.基于 logcheck 的 logtail2(http://logcheck.org)

Pygtail reads log file lines that have not been read. It will even handle log files that have been rotated. Based on logcheck's logtail2 (http://logcheck.org)

这篇关于如何在 Python 中跟踪日志文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆