python分析nginx自定义日志

# -*- coding:utf-8 -*-

import datetime
import re

logfile = '''192.168.23.43 - 2017-12-14:00:14:41 /seeyon/index.jsp?- 301 364146089 1 - - 316 0.000 GET HTTP/1.1 oa.example.com 80 - 178 Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/601.7.7 (KHTML, like Gecko) - - - - -'''
pattern = '''(?P<remote>[\d\.]{7,}) - (?P<datetime>[\d\-\:]+) (?P<request>[^\d]+) (?P<status>[\d]+) (?P<size>[\d]+) ([\d]+) - - ([\d]+) ([\d\.]+) (?P<mothod>[\w]+) (?P<protocol>[\w\.\d\/]+) (?P<host>[\w\.]+) (?P<port>[\d]+) - ([\d]+) (?P<useragent>[\d\w\.\/]+) \((?P<machine>[^\(]+)\) ([\w\d\.\/]+) \(([^\(]+)\) - - - - -'''
regex = re.compile(pattern)

def extract(line):
matcher = regex.match(line)
if matcher:
return {k:ops.get(k, lambda x:x)(v) for k, v in matcher.groupdict().items()}

'''
def convent_time(timestr):
fmtstr = "%Y-%m-%d:%H:%M:%S"
dt = datetime.datetime.strptime(timestr,fmtstr)
return dt
上面def的功能 == 下面lambda的功能
lambda timestr:datetime.datetime.strptime(timestr,"%Y-%m-%d:%H:%M:%S")
'''
ops = {
'datetime':lambda timestr:datetime.datetime.strptime(timestr,"%Y-%m-%d:%H:%M:%S"),
'status':int,
'size':int,
}

print(extract(logfile))

 

datetime有点问题,未解决!

结果:

 

posted @ 2017-12-18 14:55  勤奋的雪雪大人  阅读(306)  评论(0编辑  收藏  举报