使用Python进行Syslog实时监控

use*_*288 2 python ubuntu logging monitoring syslog

我在ubuntu的syslog文件中有日志。我可以查看来自python的新消息,还是始终需要打开/关闭syslog文件?谢谢

mpa*_*paf 5

这是您要执行的操作(使用生成器):

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
Run Code Online (Sandbox Code Playgroud)

它的工作方式类似于“ tail -f”。该代码来自此处:http : //www.dabeaz.com/generators/Generators.pdf(第39页)。另外,类似的SO问题:

使用python写入日志文件时读取