使用Python进行Syslog实时监控

时间:2014-01-21 07:13:22

标签: python ubuntu logging monitoring syslog

我在ubuntu上记录了我的syslog文件。我可以从python中查看新消息,还是总是需要打开/关闭我的syslog文件? 感谢

1 个答案:

答案 0 :(得分:4)

这是你如何做到的(使用发电机):

import time

def follow(syslog_file):
    syslog_file.seek(0,2) # Go to the end of the file
    while True:
        line = syslog_file.readline()
        if not line:
            time.sleep(0.1) # Sleep briefly
            continue
        yield line

它就像'tail -f'。代码取自此处:http://www.dabeaz.com/generators/Generators.pdf(第39页)。此外,类似的SO问题:

Read from a log file as it's being written using python

相关问题