app需要打点记录,都是记在服务端的log文件里,即使用linux命令
可以实时查到是否有关键字的出现,但是不方便,用python优化下。
主要是 基于 tail 命令的。

#coding=UTF-8
#!/usr/bin/python
import time
import subprocess
import re
import os
filedir = '/data/work/log/logs/applog/'
regx = "\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d,\d\d\d"
regx2 = r'g_action=[a-z]+_[a-z]+'

#关键字汇总,记录打点的关键字,随时可添加新的打点需求。主要能输出中文,看起来方便。
actkeydict = {
                 "cart_otherNeeds":"购物车-其他需求",
                 "preorderIllustrate":"新鲜预定介绍",
                 "cart_otherNeeds_confirm":"购物车-其他需求-确认按钮",
                 "custServiceFeedback":"我的-客服反馈",
                 "custServiceMobile":"我的-客服反馈-客户电话"
    }
act_type_list=[]
for key in actkeydict:
    act_type_list.append(key)#生成关键字的列表
while True:
    this_H = time.strftime("%H")
    filename =filedir+'app.click.log.'+ time.strftime("%Y%m%d")+this_H#获得文件名
    command = 'tail -f '+ filename + ' grep "andr201609020938078d02c87576484adc873a7696f3ace"'
    print command
    if os.path.exists(filename):
        popen = subprocess.Popen(command,stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)
        pid = popen.pid
        while True:
            line = popen.stdout.readline().strip()
            for act_type in act_type_list:
                actType="actionType="+act_type+"&"
                if line:
                    if actType in line:
                        print re.findall(regx,line),"actionType="+act_type,actkeydict[act_type],re.findall(regx2,line)
            if time.strftime("%H") > this_H:
                print "start next hour log"
                popen.kill(pid)
                break
    else:
        time.sleep(5)

执行效果:
这里写图片描述

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐