网2112 发表于 2016-10-8 08:47:59

Python 实现单笔业务日志完整提取

#!/usr/bin/python
# -*- coding:utf-8 -*-
#auth kxr by 2019.9.12
import re
import os
import time
import cx_Oracle
print '############CBOSS测试环境日志提取脚本##########'
print '##########仅支持当天的流水和网状网日志#########'
print '####提取成功将在当前目录生成保存日志的文件#####'
while True:
input1=raw_input("业务类型:(落地'y',上发'n'):\n")
if any(input1 == i for i in ['n','N']):
start_tag='准备开始处理请求'
end_tag=['结束实时上发请求处理','实时上发CBOSS异常']
break
elif input1 in ('y','Y') :
start_tag="Begin of HttpServletRequest"
end_tag=['落地业务受理过程中发生异常','End of HttpServletRequest']
break
else :pass
while True:
input2=raw_input("唯一关键字(transido,opr_num,seq...):\n")
if input2:
key_word=input2.strip()
break
line_num=0
th_num=None
match=''
f_log=[]
b_log=[]
c_log=[]
log_file='/app/aicboss/webapp/log/cboss_n2n.log'
file=open(log_file,'r')
while True:
line=file.readline()
line_num+=1
if key_word in line:
ofs=file.tell()
b_log.append(line)
file.seek(0)
match=re.search(r'httpWorkerThread-+-+',line)
lines=file.readlines()[::-1]
if match:
th_num=match.group()
for sub_line in lines:
if th_num:
if start_tagin sub_line:
f_log.append(sub_line)
break
elif th_num in sub_line:
f_log.extend(c_log)
c_log=[]
f_log.append(sub_line)
elif 'httpWorkerThread' not in sub_line:
c_log.append(sub_line)
else:
c_log=[]
elif 'httpWorkerThread'in sub_line:
match=re.search(r'httpWorkerThread-+-+',sub_line)
th_num=match.group()
f_log.append(sub_line)
else:
f_log.append(sub_line)
file.seek(ofs,0)
tag=True
for sub_line in file:
if any(t in sub_line for t in end_tag):
b_log.append(sub_line)
break
elif th_num in sub_line:
b_log.append(sub_line)
tag=True
elif 'httpWorkerThread' not in sub_line:
iftag : b_log.append(sub_line)
else:tag=False
newf_name=key_word+'.log'
path=os.getcwd()
new_file=open(os.path.join(path,newf_name),'w')
f_log.reverse()
new_file.writelines(f_log)
new_file.writelines(b_log)
file.close()
new_file.close()
print 'log_ref complete ! \nbeging ref msg...'
break
if not line :
print 'file scan end !key_word no found !'
break
'''
if 'newf_name' in dir():
file=open(os.path.join(path,newf_name))
for line in file:
if 'DoneCode' in line:
match=re.search(r'DoneCode.*+',line)
if match:
donecode=re.search(r'+',match.group()).group()
break
file.close()
if 'donecode' in dir():
table_pos=time.strftime("%Y%m",time.localtime())
table_name='cboss.xml_info_'+table_pos
conn = cx_Oracle.connect('YAXIN_CESHI/bqg776^*@ 172.16.9.29/yydbtest')
cursor = conn.cursor()
req_msg=['请求报文:']
rep_msg=['应答报文:']
msgf_name=key_word+'.xml'
sql="SELECT actioncode,xml_data from "+table_name+" where done_code=:done_code"
param=
cursor.execute(sql,param)
rows = cursor.fetchall()
for row in rows:
if row==0:
req_msg.append(row.read())
elif row==1:
rep_msg.append(row.read())
else:pass
msg_file=open(os.path.join(path,msgf_name),'w')
msg_file.writelines(map(lambda x :x+'\n',req_msg))
msg_file.writelines(map(lambda x :x+'\n',rep_msg))
msg_file.close()
cursor.close()
conn.close()
'''
if 'newf_name' in dir():
print '日志文件已生成:%s'%(newf_name)
#if 'msgf_name' in dir():
#    print '报文文件已生成:%s'%(msgf_name)
#172.16.9.29


页: [1]
查看完整版本: Python 实现单笔业务日志完整提取