这篇文章上次修改于 861 天前,可能其部分内容已经发生变化,如有疑问可询问作者。
注:原创,转载请注明来处
一、开启thinkphp日志
index.php 文件设置 define('APP_DEBUG',True);
debug.php配置文件设置 LOG_LEVEL 为 DEBUG,SQL
index.php最后么加上如下代码
$params = "[params]:\r\n";
$params .= 'POST:';
foreach ( $_POST as $k => $v ){
$params .= $k.'='.$v.'&';
}
$params .= "\r\n";
$params .= 'GET:';
foreach ( $_GET as $k => $v ){
$params .= $k.'='.$v.'&';
}
trace($params, '', 'DEBUG', TRUE);
thinkphp的日志结构如下:
[ 2016-12-21T14:35:17+08:00 ] 112.64.13.228 /Appbus/v1/Index/getIndexData
SQL: SHOW COLUMNS FROM `**` [ RunTime:0.0007s ]
SQL: SHOW COLUMNS FROM `***` [ RunTime:0.0005s ]
SQL: SELECT * FROM `***` WHERE `de**` = '362c47a**' AND `se**` = '5859**' LIMIT 1 [ RunTime:0.0004s ]
SQL: SHOW COLUMNS FROM `**` [ RunTime:0.0007s ]
DEBUG: [params]:
POST:
GET:
二、 pyinotify 监听日志文件的改动,python分析日志文件并打印处理
#!/usr/bin/python
#encoding:utf-8
import sys
import time
import re
import string
import pyinotify
import os
from pyinotify import IN_DELETE, IN_CREATE, IN_MODIFY
project=sys.argv[1]
module=sys.argv[2]
keyword=sys.argv[3]
logfile='/data/**/**/%s/application/Runtime/Logs/%s/%s.log' % (project, module, time.strftime('%y_%m_%d'))
def sqllog(logfile):
logarr = []
f = open(logfile, 'r')
logline=''
sl=0
index=0
for line in f.readlines():
if re.match('^\[.*', line.strip()):
if keyword in line.strip() :
sl=1
tmp=[]
tmp.append(line)
logarr.append(tmp)
index=index+1
else :
sl=0
if sl == 1 :
ei=index-1
if re.match('^SQL.*', line) :
logarr[ei].append(line)
if re.match('^POST.*', line) :
logarr[ei].append(line)
if re.match('^GET.*', line) :
logarr[ei].append(line)
return logarr
def prlog(logarr):
loglen=len(logarr) - 1
llen=len(logarr[loglen]) - 1
postparam = 'POST:--'
getparam = 'GET:--'
i=0
print "\r\n"
postparam = logarr[loglen][llen-1]
getparam = logarr[loglen][llen]
logarr[loglen].pop(llen)
logarr[loglen].pop(llen-1)
for log in logarr[loglen] :
if i == 0:
print '\033[1;33;40m'
else :
mt=re.findall(r'.*RunTime\:(.+)s.*', log )
if string.atof(mt[0]) >= 1 :
print '\033[1;31;40m'
print '【SLOW_SQL】'
else:
print '\033[1;32;40m'
print log
if i==0 :
print postparam
print getparam
i=i+1
print '\033[0m'
class MyEventHandler(pyinotify.ProcessEvent):
def __init__(self, logfile, long):
self.logfile=logfile
self.long = long
def process_IN_MODIFY(self, event):
logarr = sqllog(self.logfile)
loglen = len(logarr) - 1
if ( loglen != self.long ) :
prlog(logarr)
self.long = loglen
def listenChange(logfile):
# watch manager
wm = pyinotify.WatchManager()
mask = IN_CREATE | IN_MODIFY
wm.add_watch(logfile, mask, rec=True)
# event handler
logarr = sqllog(logfile)
loglen = len(logarr) - 1
eh = MyEventHandler(logfile, loglen)
# notifier
notifier = pyinotify.Notifier(wm, eh)
notifier.loop()
if __name__ == '__main__':
listenChange(logfile)
python文件命令为 sqllog
chmod +x sqllog
将sqllog移动到/usr/local/bin 目录下
三、执行测试
#jkd9.0是项目目录名称 Appbus1是模块目录名称 Index/getIndexData 是你要监听的链接包含的字符串
sqllog jkd9.0 Appbus1 Index/getIndexData
当有包含Index/getIndexData 的请求的时候,会监控的日志文件做了修改,把分析结果打印出来,结果如下:
没有评论