【数据平台】Python解析Ngnix日志
生活随笔
收集整理的這篇文章主要介紹了
【数据平台】Python解析Ngnix日志
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
場(chǎng)景:Ngnix格式具有一定格式,通過python的正則表達(dá)式讀取日志中每行的字段。
Python正則表達(dá)式參考:https://docs.python.org/2/library/re.html
代碼參考:
# -*- coding: utf-8 -*- ''' Created on 2018年1月4日@author: Jason.F @summary: Ngnix log parse ''' import time import re class NgnixLogParse(object):def __init__(self,logline):self.logline=logline'''log_format main '$remote_addr - $remote_user [$time_local] "$request" ''$status $body_bytes_sent "$http_referer" ''"$http_user_agent" "$http_x_forwarded_for" "$upstream_addr" "$request_time" "$upstream_response_time" $host';'''def logparse(self):#正則表達(dá)式ip = r"?P<ip>[\d.]*"#timelocal = r"?P<timelocal>\[.*\]" #拆解成date、month、year、log_timeuser =r"?P<user>\S+"date = r"?P<date>\d+"month = r"?P<month>\w+"year = r"?P<year>\d+"log_time = r"?P<time>\S+"#request = r"?P<request>[^\"]*" #拆解成method、request、portocalmethod = r"?P<method>\S+"request = r"?P<request>\S+"protocal=r"?P<protocal>\S+"status = r"?P<status>\d+"bodyBytesSent = r"?P<bodyBytesSent>\d+"referer=r"?P<referer>\S+"user_agent = r"?P<user_agent>[^\"]*"forwardedfor=r"?P<forwardedfor>\S+"upstream_addr = r"?P<upstream_addr>.*"requesttime=r"?P<requesttime>.*"responsetime=r"?P<responsetime>.*"host = r"?P<host>\S+"p=re.compile(r"(%s)\s-\s(%s)\s\[(%s)/(%s)/(%s)\:(%s)\s[\S]+\]\s\"(%s)\s(%s)\s(%s)\"\s(%s)\s(%s)\s\"(%s)\"\s\"(%s)\"\s\"(%s)\"\s\"(%s)\"\s\"(%s)\"\s\"(%s)\"\s(%s) " \%(ip,user,date,month,year,log_time,method,request,protocal,status,bodyBytesSent,referer,user_agent,forwardedfor,upstream_addr,requesttime,responsetime,host),re.VERBOSE)m = re.findall(p, self.logline)return mif __name__ == "__main__": start = time.clock() logline='11.11.7.21 - - [22/Nov/2017:00:28:46 +0800] "POST /xyz/qn_cb HTTP/1.1" 200 218 "-" "qiniu-callback/1.0" "-" "1.25.69.11:8080" "0.008" "0.008" x.163.com'nlp=NgnixLogParse(logline)m=nlp.logparse()print (m)print (m[0][6])end = time.clock() print('finish all in %s' % str(end - start))執(zhí)行結(jié)果: [('11.11.7.21', '-','22', 'Nov', '2017', '00:28:46', 'POST', '/xyz/qn_cb', 'HTTP/1.1', '200', '218', 'qiniu-callback/1.0', '-','1.25.69.11:8080', '0.008', '0.008', 'x.163.com')] /xyz/qn_cb finish all in 0.00115241167476
總結(jié)
以上是生活随笔為你收集整理的【数据平台】Python解析Ngnix日志的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【正一专栏】西班牙国家德比不再激情四射可
- 下一篇: 【正一专栏】魔鬼圣诞赛程结束争四利物浦占