Python自动监控错误日志

平时在查看日志的时候打开满屏的日志,看上去有点凌乱。于是写个Python脚本过滤出想要看的错误的日志。直接上脚本

脚本示例

 1 def read_log(logname,keyword):
 2     tell = 0#文件指针
 3     i=1#行号
 4     while True:
 5         f = open(logname)
 6         f.seek(tell)#移动文件指针
 7         res=[]#存所有的结果
 8         for line in f:
 9             if keyword in line:
10                 print(line)
11                 res.append('行号【%s】 内容:%s 
'%(i,line))
12                 i+=1
13         tell = f.tell()
14         f1=open('res.txt','a')
15         f1.writelines(res)
16         f1.flush()
17 
18 read_log('catalina.out', 'ERROR')

以上的脚本只是剥离出错误的日志行,具体显示如下

原文地址:https://www.cnblogs.com/hanxiaobei/p/8626263.html