Python解析日志文件以实时提取事件 [英] Python parsing log file to extract events in real time

查看:1214
本文介绍了Python解析日志文件以实时提取事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个将消息记录到文件的过程.

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

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

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.

谢谢.

推荐答案

C程序通常寻求到当前位置以清除所有文件结束"标志.但是正如 @ 9000正确指出的所示,python显然可以解决这一问题,因此即使您重复读取同一文件,即使它已到达文件末尾.

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.

不过,您可能需要处理不完整的行.如果您的应用程序按段编写日志,那么您要确保处理整行而不是整行.下面的代码将完成此操作:

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)

这篇关于Python解析日志文件以实时提取事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆