在httpd.conf apache的转换添加此记录到json。
LogFormat "{\"time\":\"%t\", \"remoteIP\" :\"%a\", \"host\": \"%V\", \"request_id\": \"%L\", \"request\":\"%U\", \"query\" : \"%q\", \"method\":\"%m\", \"status\":\"%>s\", \"userAgent\":\"%{User-agent}i\", \"referer\":\"%{Referer}i\" }" json_log
CustomLog /var/log/apache_access_log json_log
CustomLog "|/usr/bin/python -u apacheLogHandler.py" json_log
现在你看到你的json格式的access_logs。 使用下面的python代码来解析不断更新的json日志。
apacheLogHandler.py
import time
f = open('apache_access_log.log', 'r')
for line in f: # read all lines already in the file
print line.strip()
# keep waiting forever for more lines.
while True:
line = f.readline() # just read more
if line: # if you got something...
print 'got data:', line.strip()
time.sleep(1)
此行的元素是你感兴趣的? (所有这些?) –
随着线条稍微改变,我希望它们能够以这种确切格式进行阅读:) – ogward
您误解了我的意思是,您想要从每一行中提取什么?日期? IP?所有的? –