日志文件分割:将包含不同关键字的行写入到不同的文件

By | 2013/01/11

实现功能:

日志文件分析,找出包含关键字的行,并写入到新的文件中;
逐行匹配,关键字可以同时传入多个(使用“|”分割),也可以一行匹配多个关键字(使用&分割)

例如: > analysislog java|python&2.7
就会找出包含java的行,并写入java.log文件中;还会找出包含python并且包含2.7关键字的行,并写入python&2.7.log的文件中

 

#-*- coding:utf8 -*-
 
 import os, sys, time
 
 
 def analysislog(filepath, keyword):
     if not os.path.exists(filepath):
         print "File not found !"
         raw_input("")
         sys.exit()
 
     keywordlist = keyword.split("|")
     fileobjlist = {}
     for key in keywordlist:
         fileobjlist[key] = open(key+".log", 'w')
     
     logfile = open(filepath, 'r')
     
     while 1:
         logline = logfile.readline()
 
         if logline == "":
             break
 
         for key in keywordlist:
             if key.find("&") <> -1:
                 andkeylist = key.split("&")
                 andflag = 1
                 for andkey in andkeylist:
                     if logline.find(andkey) == -1:
                         andflag = 0
                 if andflag:
                     fileobjlist[key].write(logline)
 
             else:
                 if logline.find(key) <> -1:
                     fileobjlist[key].write(logline)
     for key in keywordlist:
         fileobjlist[key].close()
 
 
     logfile.close()
 
 
 def appendtonewfile(logstr, newlogname):
     newlogfile = open(os.path.join(os.getcwd(), newlogname),"a")
 
     newlogfile.write(logstr)
 
     newlogfile.close
 
 
 
 if __name__ == "__main__":
     if len(sys.argv) == 1 :
         logfilepath = raw_input("Please input the path of the log -> ")
         keywords = raw_input("Please input the keywords(split by |) -> ")
     elif len(sys.argv) == 2 :
         logfilepath = sys.argv[1]
         keywords = raw_input("Please input the keywords(split by |) -> ")
     elif len(sys.argv) == 3 :
         logfilepath = sys.argv[1]
         keywords = sys.argv[2]
     else :
         print "Illegal Parameters...\n    AnalysisLog.py [logpath] [keywords] "
         raw_input("")
         sys.exit()
 
     
     print "Log Path: %s"%logfilepath
     print "Keywords: %s"%keywords.replace("|", " ")
     
     starttime = time.time()
     analysislog(logfilepath, keywords)
     endtime = time.time()
     print "Time used: ",(endtime - starttime)," s"

 

PS: 可以使用 pyinstall 打包成exe程序。

One thought on “日志文件分割:将包含不同关键字的行写入到不同的文件

Leave a Reply

Your email address will not be published. Required fields are marked *