日志分析之信息提取
如下是最常见的日志,nginx、tomcat等WEB Sever 都会产生这样的日志。如何提取出数据?
代码实现
line = '''183.60.212.153 - -[19/Feb/2013:10:23:29 +0800] \
"GET /o2o/media.html?menu=3 HTTP'1.1" 200 26691 "-"\
"Mozila/5.0(compatible;EasouSpider;+http://www.easou.com/search/spider.html"'''
CHARS = set("\t")
def makekey (line:str):
start = 0
skip = False
for i, c in enumerate(line):
if not skip and c in '"[': # [ 或 第一个引号
start = i +1
skip = True
elif skip and c in '"]': # 第二个引号 或 ]
skip = False
yield line [start:i]
start = i + 1
continue
if skip: # 如果遇到[ 或第一个引号就跳过
continue
if c in CHARS:
if start == i:
start = i +1