Linux Python解析日志文件实时提取事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11908919/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 14:03:32  来源:igfitidea点击:

Python parsing log file to extract events in real time

pythonlinuxlogging

提问by Soumya Simanta

I've a process that is logging messages to a file.

我有一个将消息记录到文件的进程。

I want to implement another process (in Python) that parses these logs (as they are written to the file), filters the lines that I'm interested in and then performs certain actions based on the state of the first process.

我想实现另一个进程(在 Python 中)来解析这些日志(当它们被写入文件时),过滤我感兴趣的行,然后根据第一个进程的状态执行某些操作。

I was wondering before I go ahead and write something on my own if there is a library in Python that does something like this.

在我继续自己写一些东西之前,我想知道 Python 中是否有一个库可以做这样的事情。

Also, ideas regarding how implement something like this Python would be appreciated.

此外,关于如何实现这样的 Python 的想法将不胜感激。

Thanks.

谢谢。

采纳答案by MvG

C programs usually seek to the current position to clear any “end of file” flags. But as @9000 correctly pointed out, python apparently takes care of this, so you can read from the same file repeatedly even if it has reached end of file.

C 程序通常会寻找当前位置以清除任何“文件结束”标志。但是正如@9000 正确指出的那样,python 显然会处理这个问题,因此即使它已到达文件末尾,您也可以重复读取同一个文件。

You might have to take care of incomplete lines, though. If your application writes its log in pieces, then you want to make sure that you handle whole lines, and not those pieces. The following code will accomplish that:

不过,您可能需要处理不完整的行。如果您的应用程序将其日志分块写入,那么您要确保处理整行,而不是那些部分。以下代码将实现这一点:

f = open('some.log', 'r')
while True:
    line = ''
    while len(line) == 0 or line[-1] != '\n':
        tail = f.readline()
        if tail == '':
            time.sleep(0.1)          # avoid busy waiting
            # f.seek(0, io.SEEK_CUR) # appears to be unneccessary
            continue
        line += tail
    process(line)

回答by 9000

No need to run tail -f. Plain Python files should work:

无需运行tail -f。普通的 Python 文件应该可以工作:

with open('/tmp/track-this') as f:
  while True:
    line = f.readline()
    if line:
      print line

This thing works almost exactly like tail -f. Check it by running in another terminal:

这东西的工作原理几乎完全一样tail -f。通过在另一个终端中运行来检查它:

echo "more" >> /tmp/track-this
# alt-tab here to the terminal with Python and see 'more' printed
echo "even more" >> /tmp/track-this

Don't forget to create /tmp/track-thisbefore you run the Python snippet.

/tmp/track-this在运行 Python 代码段之前不要忘记创建。

Parsing and taking appropriate actions are up to you. Probably long actions should be taken in separate threads/processes.

解析和采取适当的行动取决于你。可能应该在单独的线程/进程中执行长时间的操作。

Stop condition is also up to you, but plain ^Cworks.

停止条件也取决于您,但很简单^C

回答by Soumya Simanta

Thanks everyone for the answers. I found this as well. http://www.dabeaz.com/generators/follow.py

谢谢大家的回答。我也发现了这个。 http://www.dabeaz.com/generators/follow.py