需求
遍历logfile文件夹,除去pass目录下的文件都查一遍
要求:log文件名要求以"log_"开头,类型为txt文件
遍历log文件,key_value_XXX.txt文件中包含的字符串,都存到result_开头的txt中
代码
# 0_log_auto_analysis.bat
@echo off
for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (
set "DEL=%%a"
)
@echo ------------------- python -----------------------
python auto_log_analysis.py
@echo ------------------- EXIT ---------------------------
echo=
pause
exit
::----------- delay_ms sub-function (unit : 10 ms) --------------------
:delay_ms
@echo off
if "%1"=="" goto :eof
set delay_msTime=%1
set TotalTime=0
set NowTime=%time%
:: read init time, format : 13:01:05.95
:delay_ms_continue
set /a minute1=1%NowTime:~3,2%-100
set /a second1=1%NowTime:~-5,2%%NowTime:~-2%0-100000
set NowTime=%time%
set /a minute2=1%NowTime:~3,2%-100
set /a second2=1%NowTime:~-5,2%%NowTime:~-2%0-100000
set /a TotalTime+=(%minute2%-%minute1%+60)%%60*60000+%second2%-%second1%
if %TotalTime% lss %delay_msTime% goto delay_ms_continue
goto :eof
:ColorText
@echo off
<nul set /p ".=%DEL%" > "%~2"
findstr /v /a:%1 /R "^$" "%~2" nul
del "%~2" > nul 2>&1
goto :eof
# auto_log_analysis.py
import os
def get_front_2_last (line, front, last):
begin=line.find(front)+len(front)
end=line.find(last)
ss=line[begin:end]
# print(ss)
return ss
def apply_file (log_name, log_key):
file_log=open(log_name,encoding='utf-8',errors='gbk')
# file_key_value=open("log1.txt",errors='ignore')
file_key_value=open(log_key)
#file_key_result=open("key_result_usecase.txt","a")#use w just save 1 char
result_name=log_name.replace('log_','result_')
print(result_name)
print(log_name)
# result_name="log_file/result_log1.txt"
file_key_result=open(result_name,"w")#use w just save 1 char
file_key_result.truncate() #delete now contect
line_log=file_log.readline()
while line_log:
#func2{
file_key_value.seek(0, 0)
key_value=file_key_value.readline()
while key_value:
key=key_value.strip()
result_a=key in line_log
if result_a == True :
break
key_value=file_key_value.readline()
temp="adev_open_input_stream:" in line_log
if temp == True :
print("xxx")
file_key_result.write("\n")
if result_a == True :
file_key_result.write(line_log) #copy one line
line_log=file_log.readline() #read the next line
#func2}
file_log.close
file_key_value.close
file_key_result.close
return
# func get java file path
def get_file_path(path, file_type, func, log_key):
for root, dirs, files in os.walk(path):
for f in files:
# ignore "pass" dir
a = "pass" in root
if a == True :
continue
temp1=0
temp2=0
temp=0
temp1=file_type in f # file type is txt
temp2="log_" in f # file is log not result
temp=temp1+temp2
if temp == 2 :
# print('file_path: ',file_path)
file_path=root+'/'+f
print(file_path)
print('file_name: ',f)
apply_file(file_path, log_key)
return
dir_path="logfile"
file_type_a=".txt"
function_a="apply_file"
log_key="cmd/key_value_time.txt"
get_file_path(dir_path, file_type_a, function_a,log_key)
cmd目录下存放关键字txt,可以修改代码中的log_key选择。
logfile文件中有pass文件夹,用于存放过往的文件,pass文件夹不会被遍历
log_开头的为你需要处理的log
result_打头的是生成的结果