python监控一个非阻塞的日志文件

roy*_*g86 4 python logging generator tail

我有一个测试存根,它将向系统日志写入几条日志消息。

但是,这个系统日志也会被许多其他应用程序更新。所以,基本上,我想做一个tail -f system.log | grep "application name"只获取适当的日志消息。

我在看 dbaez 生成器技巧,我正在尝试将http://www.dabeaz.com/generators/follow.py和http://www.dabeaz.com/generators/apachelog.py结合起来

所以,在我的__main__(),我有这样的事情:

try:
   dosomeprocessing()     #outputs stuff to the log file
Run Code Online (Sandbox Code Playgroud)

在 dosomeprocessing() 中,我运行一个循环,对于每个循环,我想查看是否有任何由我的应用程序引起的新日志消息,不一定将其打印出来,而是将它们存储在某处以进行一些验证。

    logfile = open("/var/adm/messages","r")
    loglines = follow(logfile)
    logpats = r'I2G(JV)'
    logpat = re.compile(logpats)
    groups = (logpat.match(line) for line in loglines)
    for g in groups:
        if g:
            print g.groups()
Run Code Online (Sandbox Code Playgroud)

日志看起来像:

Feb  4 12:55:27 Someprocessname.py I2G(JV)-300[20448]: [ID 702911   local2.error] [MSG-70047] xxxxxxxxxxxxxxxxxxxxxxx 
 Feb  4 12:55:27 Someprocessname.py I2G(JV)-300[20448]: [ID 702911  local2.error] [MSG-70055] xxxxxxxxxxxxxxxxxxxxxxx
Run Code Online (Sandbox Code Playgroud)

除了很多其他的gobblygook。

现在,它被困在 for g 组中:

我对 python 和异步编程比较陌生。理想情况下,我希望能够让尾部与主进程并行运行,并在每个循环中读取新数据。

如果我需要添加更多信息,请告诉我。

Dmi*_*ylo 7

我建议您使用watchdog或pyinotify来监控日志文件的更改。

另外,我建议记住您阅读的最后一个位置。收到 IN_MODIFY 通知后,您可以从最后一个位置读取到文件末尾并再次应用您的循环。此外,如果文件被截断,当它大于文件大小时,将最后一个位置重置为 0。

这是示例:

import pyinotify
import re
import os


wm = pyinotify.WatchManager()
mask = pyinotify.IN_MODIFY


class EventHandler (pyinotify.ProcessEvent):

    def __init__(self, file_path, *args, **kwargs):
        super(EventHandler, self).__init__(*args, **kwargs)
        self.file_path = file_path
        self._last_position = 0
        logpats = r'I2G\(JV\)'
        self._logpat = re.compile(logpats)

    def process_IN_MODIFY(self, event):
        print "File changed: ", event.pathname
        if self._last_position > os.path.getsize(self.file_path):
            self._last_position = 0
        with open(self.file_path) as f:
            f.seek(self._last_position)
            loglines = f.readlines()
            self._last_position = f.tell()
            groups = (self._logpat.search(line.strip()) for line in loglines)
            for g in groups:
                if g:
                    print g.string


handler = EventHandler('some_log.log')
notifier = pyinotify.Notifier(wm, handler)

wm.add_watch(handler.file_path, mask)        
notifier.loop()
Run Code Online (Sandbox Code Playgroud)