#!/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 ("Business type: (Landing ' y ', on ' n '): \ n")
If any (input1 = = I for i in [' N ', ' n ']):
Start_tag= ' ready to start processing requests '
end_tag=[' End real-time request processing ', ' real-time Cboss exception ']
Break
Elif input1 in (' Y ', ' y '):
Start_tag= "Begin of HttpServletRequest"
End_tag=[' An exception occurred during the acceptance process of the landing business ', ' End of HttpServletRequest ']
Break
Else:pass
While True:
Input2=raw_input ("Unique keyword (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=[' request message: '
rep_msg=[' response message: ']
msgf_name=key_word+ '. Xml '
Sql= "Select Actioncode,xml_data from" +table_name+ "where done_code=:d One_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 ' log file generated:%s '% (newf_name)
#if ' Msgf_name ' in Dir ():
# print ' message file generated:%s '% (msgf_name)
#172.16.9.29
This article is from the "Operation and Maintenance Learning Road" blog, please make sure to keep this source http://kk15320.blog.51cto.com/4848160/1858704
Python implements a single business log complete extraction