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-[0-9]+-[0-9]+',line)

lines=file.readlines()[line_num-1000 if line_num-1000 > 0 else 0 :line_num-1][::-1]

if match:

th_num=match.group()

for sub_line in lines:

if th_num:

if start_tag  in 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-[0-9]+-[0-9]+',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:

if  tag : 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.*[0-9]+',line)

if match:

donecode=re.search(r'[0-9]+',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=[donecode]

cursor.execute(sql,param)

rows = cursor.fetchall()

for row in rows:

if row[0]==0:

req_msg.append(row[1].read())

elif row[0]==1:

rep_msg.append(row[1].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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章