[英]Read from a log file as it's being written using python
我正在尝试找到一种使用 python 实时读取日志文件的好方法。 我想在写入时一次处理一个日志文件中的行。 不知何故,我需要继续尝试读取文件,直到它被创建,然后继续处理行,直到我终止进程。 有没有合适的方法来做到这一点? 谢谢。
从第 38 页开始查看此 PDF ,~幻灯片 I-77,您将找到所需的所有信息。 当然,其余的幻灯片也很精彩,但那些专门针对您的问题:
import time
def follow(thefile):
thefile.seek(0,2) # Go to the end of the file
while True:
line = thefile.readline()
if not line:
time.sleep(0.1) # Sleep briefly
continue
yield line
你可以尝试这样的事情:
import time
while 1:
where = file.tell()
line = file.readline()
if not line:
time.sleep(1)
file.seek(where)
else:
print line, # already has newline
示例是从这里提取的。
由于这是 Python 并带有日志标记,因此还有另一种可能性。
我假设这是基于 Python 记录器,基于 logging.Handler。
您可以创建一个获取(命名)记录器实例的类并覆盖emit
函数以将其放到 GUI 上(如果您需要控制台,只需将控制台处理程序添加到文件处理程序中)
例子:
import logging
class log_viewer(logging.Handler):
""" Class to redistribute python logging data """
# have a class member to store the existing logger
logger_instance = logging.getLogger("SomeNameOfYourExistingLogger")
def __init__(self, *args, **kwargs):
# Initialize the Handler
logging.Handler.__init__(self, *args)
# optional take format
# setFormatter function is derived from logging.Handler
for key, value in kwargs.items():
if "{}".format(key) == "format":
self.setFormatter(value)
# make the logger send data to this class
self.logger_instance.addHandler(self)
def emit(self, record):
""" Overload of logging.Handler method """
record = self.format(record)
# ---------------------------------------
# Now you can send it to a GUI or similar
# "Do work" starts here.
# ---------------------------------------
# just as an example what e.g. a console
# handler would do:
print(record)
我目前正在使用类似的代码添加一个 TkinterTreectrl.Multilistbox 以在运行时查看记录器输出。
Off-Side:记录器仅在初始化后立即获取数据,因此如果您想获得所有数据,则需要在一开始就对其进行初始化。 (我知道这是预期的,但我认为值得一提。)
也许你可以做一个系统调用
tail -f
使用 os.system()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.