码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2016-10-05 01:12:51      阅读:245      评论:0      收藏:0      [点我收藏+]

标签: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


本文出自 “运维学习之路” 博客,请务必保留此出处http://kk15320.blog.51cto.com/4848160/1858704

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

标签:python   日志处理   

原文地址:http://kk15320.blog.51cto.com/4848160/1858704

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!