技术宅,fat-man

增加语言的了解程度可以避免写出愚蠢的代码

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

python解析日志的代码

复制代码
#!/usr/bin/env python

import re
import os
import os.path
import gzip
import json
from urlparse import *
import datetime
import time

DICT = {}

def print_time(s):
        #print (datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S'))
        print datetime.datetime.now().strftime("%H:%M:%S.%f") + " " + str(s)

def remove_flag(s):
        return s.replace("[","").replace("]","")

def remove_session_prefix(s):
        return s.replace("session:","")

def parse_url(url):
        a_dict = {}
        aUrl = urlparse("http://"+url)
        url_params = dict([(k,v[0]) for k,v in parse_qs(aUrl.query).items()])

        a_dict.update({"domain":aUrl.netloc})
        a_dict.update({"path":aUrl.path})
        a_dict.update({"params":url_params})
        return a_dict

def get_info(a_dict):
        new_dict = {}

        if a_dict["userid"] != "" and a_dict["userid"] != "0":
                new_dict.update({"userid":a_dict["userid"]})
        else:
                new_dict.update({"userid":a_dict["session"]})

        new_dict.update({"domain":a_dict["url"]["domain"]})
        new_dict.update({"path":a_dict["url"]["path"]})

        if "fr" in a_dict["url"]["params"]:
                new_dict.update({"fr":a_dict["url"]["params"]["fr"]})
        else:
                new_dict.update({"fr":"-"})

        if "ct" in a_dict["url"]["params"]:
                new_dict.update({"ct":a_dict["url"]["params"]["ct"]})
        else:
                new_dict.update({"ct":"-"})

        if "ac" in a_dict["url"]["params"]:
                new_dict.update({"ac":a_dict["url"]["params"]["ac"]})
        else:
                new_dict.update({"ac":"-"})

        key = json.dumps(new_dict)
        if key in DICT:
                DICT[key] += 1
                #print key + " " + str(DICT[key])
        else:
                DICT.update({key:1})


def read_logs(path):
        for item in os.listdir(path):
                f = gzip.open(path+"/"+item,"r")
                if f == None:
                        raise "program can't open this file"

                i = 0
                while True:
                        if i % 10000 == 0:print_time(i)

                        line = f.readline()
                        if i % 10000 == 0:print_time("read to memory")
                        if not line:
                                break

                        aDict = {}
                        list = re.findall('\[.*?\]',line)
                        if i % 10000 == 0:print_time("split to items")

                        urlDict = parse_url(remove_flag(list[4]))
                        if i % 10000 == 0:print_time("url fommat to dict") 
                        aDict.update({"userid":remove_flag(list[2])})
                        aDict.update({"session":remove_session_prefix(remove_flag(list[7]))})
                        aDict.update({"url":urlDict})
                        if i % 10000 == 0:print_time("create new dict") 

                        get_info(aDict)
                        if i % 10000 == 0:print_time("save and diff")

                        i += 1

                f.close()



if __name__ == "__main__":
        #try:
                read_logs("logs/20130908")
                f = open("data","w")
                for i in DICT:
                        f.write(i+" "+str(DICT[i]))
                f.close()
        #except :
        #       print "error"
复制代码

 

 

 

posted on   codestyle  阅读(284)  评论(0编辑  收藏  举报

编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述
历史上的今天:
2012-09-10 ubuntu安装启动redis
点击右上角即可分享
微信分享提示