实时读取多个日志文件

Read multiple log file in realtime

我必须从实时存在的日志文件中读取数据。我使用了 中提到的代码,但是我只能读取第一个文件,即 system.log。如何迭代读取所有文件,即首先 system.log 然后 wifi.log 并再次重复相同的过程。

import time
import glob
import threading

def follow(thefile):
   thefile.seek(0,2)
    while True:
        line = thefile.readline()
        if not line:
            time.sleep(0.1)
            continue
        yield line


 lock = threading.Lock()

 def printFile(logfile):
    loglines = follow(logfile)
    for line in loglines:
        lock.acquire()
        print (line)
        lock.release()



  if __name__ == '__main__':
      files=['system.log','wifi.log']
      for log in files:
          logfile = open(log, "r")
          t = threading.Thread(target = printFile,args = (logfile,))
          t.start()

您可以使用 asyncio 库创建一个并发函数来跟随文件的尾部然后打印它。

import asyncio

async def follow(thefile):
    thefile.seek(0,2)
    while True:
        line = thefile.readline()
        if not line:
            await asyncio.sleep(0.1)
            continue
        yield line.strip()

async def main(path):
    async for x in follow(open(path)):
        print(x)

for path in ['hej', 'med', 'dig']:
    asyncio.ensure_future(main(path))

loop = asyncio.get_event_loop()
loop.run_forever()

也可以跳过main函数和follow函数中的yield,直接在follow函数中打印输出,不用传递。