import re import sys def getmongo_overtime_log(len_time, logfile): d = {} l = [] open_list = [] with open(logfile, 'r') as f: for line in f: line_sql = re.search('^.*\[conn(\d+)\].*\s+(\d+)ms$', line) line_conn_open = re.search('^.*T(.*)\+.*#(\d+).*$', line) line_conn_close = re.search('^.*T(.*)\+.*\[conn(\d+)\].*end\sconnection.*$', line) if line_conn_open: conn_num = line_conn_open.group(2) if not d.get(conn_num): d[conn_num] = [] d[conn_num].append(line) open_list.append(conn_num) else: d[conn_num].append(line) elif line_conn_close: conn_num = line_conn_close.group(2) if not d.get(conn_num): d[conn_num] = [] d[conn_num].append(line) else: d[conn_num].append(line) if line in d[conn_num] and len(d[conn_num]) >= 3 and conn_num in open_list: for x in d[conn_num]: print x print '=' * 100 elif line_sql: use_time = line_sql.group(2) conn_num = line_sql.group(1) if int(use_time) > int(len_time): if not d.get(conn_num): d[conn_num] = [] d[conn_num].append(line) else: d[conn_num].append(line) if __name__ == '__main__': logfile = sys.argv[1] len_time = sys.argv[2] #logfile = r'C:\Users\Administrator\Desktop\mongodb_27037.log.2016-01-07T16-00-09' getmongo_overtime_log(int(len_time), logfile)