Python实现日志提取

思路:

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操作就可以了。

  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值