思路:
1.添加一个json文件配置指定日志资源路径
2.解析json文件,提取资源资源文件路径
3.正则表达式提取目标信息的所有数据
4.根据自己业务需求对目标数据进行操作
下面附上代码:
json文件内容:
config_path = r"config/config.json"
{
"logPath": "resource/zhuhai.log",
"outPath": "report/unimrcpserver.log"
}
# 读取json文件内容
fileJson = readFile(config_path)
# 提取日志资源路径
log_path = fileJson["logPath"]
# 匹配正则表达式规则
def getcont_to_new_file(path, pattern, newfile):
file = open(path, "r+", encoding="UTF-8")
newfile = open(newfile, "w", encoding="UTF-8")
lines = file.readlines()
data = []
call_id = ""
# for i in range(len(keywords)):
newfile.write("--------------------" + "\n")
for line in lines:
if len(pattern.findall(line)) > 0:
# print("lines = " ,line)
data.append(line)
newfile.write(line)
return data
pattern = re.compile(r'send http end[(](.*?)|send demo_recog_recognition_complete msg')
# 提取日志中包含http end和msg字段的所有数据并写入_original.txt文件中
data = getcont_to_new_file(log_path, pattern, original_file_name)
这样就实现完成了,最后根据自己业务需要对提取的data操作就可以了。