Python日志分析与正则表达式

程序员经常会面临日志的分析工作。而正则表达式是处理日志的必备工具。

“Line 622: 01-01 09:04:16.727 <6> [pid:14399, cpu1 dabc_pwym_task] [histbf] update_freeze_data: dabc9:bl_level=1740”
“app_log_cat new log begin”
“Line 627: 01-01 09:04:17.727 <6> [pid:14399, cpu1 dabc_pwym_task] [histbf] update_freeze_data: dabc:bl_level=1720”

 

比如,对于上面的日志,需要找到日志时间,并且要找到对应格式的数据。这里面包含的问题主要包括:

  1. 匹配工作。需要找到真正的日志,上面的第二行就不是真正的日志;
  2. 分割工作(split)。把日志按照空格进行分割,找到日志时间;
  3. 筛选工作。找到匹配的格式,从而把数字1740和1720筛选出来。

针对匹配工作,需要找到开头是 'Line' 的行。用到re的search()函数。

import re

strrs = list()
strrs.append("Line 622: 01-01 09:04:16.727 <6> [pid:14399, cpu1 dabc_pwym_task] [histbf] \
          update_freeze_data: dabc9:bl_level=1740")
strrs.append("app_log_cat new log begin")
strrs.append("Line 627: 01-01 09:04:17.727 <6> [pid:14399, cpu1 dabc_pwym_task] [histbf] \
          update_freeze_data: dabc:bl_level=1720")
regex = r'Line'
for strr in strrs:
    str_search = re.match(regex, strr)
    if str_search:
        print(True)
    else:
        print(False)

匹配结果如下

True
False
True

针对分割工作,需要找到日志时间。观察上述日志,是以空格作为分割依据。

import re

strr = 'Line 622: 01-01 09:04:16.727 <6> [pid:14399, cpu1 dabc_pwym_task] [histbf]' \
       ' update_freeze_data: dabc9:bl_level=1740'
regex = '\s'
str_split = re.split(regex, strr)
print(str_split)

分割后的输出是一个list,在其中选择时间对应的数据即可。

['Line', '622:', '01-01', '09:04:16.727', '<6>', '[pid:14399,', 'cpu1', 'dabc_pwym_task]', '[histbf]', 'update_freeze_data:', 'dabc9:bl_level=1740']

针对筛选工作,需要找到最后的数据。

import re

strr = """Line 622: 01-01 09:04:16.727 <6> [pid:14399, cpu1 dabc_pwym_task] [histbf]
          update_freeze_data: dabc9:bl_level=1740
          Line 627: 01-01 09:04:17.727 <6> [pid:14399, cpu1 dabc_pwym_task] [histbf] 
          update_freeze_data: dabc:bl_level=1720"""
regex = r'dabc\d?:bl_level=(\d+)'
str_select = re.findall(regex, strr)
print(str_select)

筛选后的结果是一个list

['1740', '1720']

 

posted on 2017-08-16 23:49  Apple@Jing  阅读(1013)  评论(1编辑  收藏  举报

导航