python解析oracle日志中的报错
作为python的简单应用,这里将oracle日志文件中的报错内容截取出来。
方案构思:经过分析日志结果,以时间戳为楔子,两个时间戳之间内容作为分块,若果分块内存在报错内容,就将两个时间戳之间的分块内容打印出来。
1 import re 2 3 def get_time(week,tmp_file): 4 time = [] 5 for line in tmp_file: 6 tmp_line = line.split() 7 lens = len(tmp_line) 8 #print(lens) 9 if lens > 3 : 10 if (tmp_line[0] in week[:]) and (int(tmp_line[2]) in range(1,32)): 11 time.append(line.strip()) 12 return time 13 14 def get_logs(time,filetxt): 15 i= 0 16 n = i+1 17 while i < len(time)-1 : 18 while time[i] == time[n]: 19 n +=1 20 if n == len(time)-1: 21 break 22 pattern = re.compile( time[i]+'(.*?)'+time[n],re.S) 23 result = re.findall(pattern,filetxt) 24 seltxt = result[0] 25 if "ORA-" in seltxt or "Errors" in seltxt or "error" in seltxt or "TNS-" in seltxt: 26 print('\n\n===>>报错起始时间:', time[i]) 27 print(''.join(result)) 28 # print(type(result)) 29 print('<<===报错终止始时间:', time[n]) 30 i = n 31 32 def main(): 33 logfile = r'D:\WorkHome\python\oracle\alert_irmsdb.log' 34 week = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'] 35 file = open(logfile) 36 filelines = file.readlines() 37 tmp_file = filelines[-200:] 38 time = get_time(week,tmp_file) 39 filetxt = "".join(tmp_file).strip() 40 get_logs(time,filetxt) 41 42 if __name__ == '__main__': 43 main()