从使用 python 写入的日志文件中读取
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3290292/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Read from a log file as it's being written using python
提问by Anon
I'm trying to find a nice way to read a log file in real time using 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.
我正在尝试找到一种使用 python 实时读取日志文件的好方法。我想在写入时一次处理一个日志文件中的行。不知何故,我需要继续尝试读取文件,直到它被创建,然后继续处理行,直到我终止进程。有没有合适的方法来做到这一点?谢谢。
采纳答案by Pablo Santa Cruz
回答by Guillaume Lebourgeois
Maybe you could do a system call to
也许你可以做一个系统调用
tail -f
using os.system()
使用 os.system()
回答by Wayne Werner
Take a look at this PDFstarting at page 38, ~slide I-77 and you'll find all the info you need. Of course the rest of the slides are amazing, too, but those specifically deal with your issue:
从第 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
回答by R4PH43L
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 emitfunction 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. (I know this is what is expected, but I think it is worth being mentioned.)
Off-Side:记录器仅在初始化后立即获取数据,因此如果您想获得所有数据,则需要在一开始就对其进行初始化。(我知道这是预期的,但我认为值得一提。)

