[英]Read from a log file as it's being written using python
I'm trying to find a nice way to read a log file in real time using python.我正在尝试找到一种使用 python 实时读取日志文件的好方法。 I'd like to process lines from a log file one at a time as it is written.
我想在写入时一次处理一个日志文件中的行。 Somehow I need to keep trying to read the file until it is created and then continue to process lines until I terminate the process.
不知何故,我需要继续尝试读取文件,直到它被创建,然后继续处理行,直到我终止进程。 Is there an appropriate way to do this?
有没有合适的方法来做到这一点? Thanks.
谢谢。
Take a look at this PDF starting at page 38, ~slide I-77 and you'll find all the info you need.从第 38 页开始查看此 PDF ,~幻灯片 I-77,您将找到所需的所有信息。 Of course the rest of the slides are amazing, too, but those specifically deal with your issue:
当然,其余的幻灯片也很精彩,但那些专门针对您的问题:
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
As this is Python and logging tagged, there is another possibility to do this.由于这是 Python 并带有日志标记,因此还有另一种可能性。
I assume this is based on a Python logger, logging.Handler based.我假设这是基于 Python 记录器,基于 logging.Handler。
You can just create a class that gets the (named) logger instance and overwrite the emit
function to put it onto a GUI (if you need console just add a console handler to the file handler)您可以创建一个获取(命名)记录器实例的类并覆盖
emit
函数以将其放到 GUI 上(如果您需要控制台,只需将控制台处理程序添加到文件处理程序中)
Example:例子:
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)
I am currently using similar code to add a TkinterTreectrl.Multilistbox for viewing logger output at runtime.我目前正在使用类似的代码添加一个 TkinterTreectrl.Multilistbox 以在运行时查看记录器输出。
Off-Side: The logger only gets data as soon as it is initialized, so if you want to have all your data available, you need to initialize it at the very beginning. Off-Side:记录器仅在初始化后立即获取数据,因此如果您想获得所有数据,则需要在一开始就对其进行初始化。 (I know this is what is expected, but I think it is worth being mentioned.)
(我知道这是预期的,但我认为值得一提。)
Maybe you could do a system call to也许你可以做一个系统调用
tail -f
using os.system()使用 os.system()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.