简体   繁体   English

从使用 python 写入的日志文件中读取

[英]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

You could try with something like this:你可以尝试这样的事情:

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

Example was extracted from here .示例是从这里提取的。

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.

相关问题 在 Python 中写入文件时从文件中读取? - Read from file while it is being written to in Python? 从正在写入的视频.avi文件中读取 - Read from video .avi file as it is being written 拦截(使用Python)数据从另一个进程写入文件 - Intercept (using Python) data being written to a file from another process 从Python中的文件中读取字典的书面列表 - Read the written list of dictionaries from file in Python 连续读取正在连续写入的 python 中的 pcap 文件 - Continuously read pcap file in python that being written continuously python 没有使用 AudioSegment.from_file 访问通过 Java 中的 FileOutputStream 写入的文件 - File written through FileOutputStream in Java is not being accessed by python using AudioSegment.from_file 使用python数据框未将数据写入文件的问题 - issue with data not being written to file using python dataframe 从正在用Python写入的打开文件句柄中读取 - Reading from open file handle that is being written to in Python Python-从Windows中编写的文本文件读取 - Python - Reading from a text file that is being written in Windows 我试图读取在我的 Python 程序中以文本形式写入的日志文件,但它返回“没有这样的文件或目录” - Im trying to read a log file which is written in text in my Python program but its returning an “No such file or directory”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM