码迷,mamicode.com
首页 > 其他好文 > 详细

log合并小工具

时间:2019-01-02 01:17:23      阅读:326      评论:0      收藏:0      [点我收藏+]

标签:image   http   read   EAP   inf   return   字典   exist   text   

在ROM升级项目中,偶尔遇到需要结合ap-log(上层日志)与kmsg-log(底层日志)一起分析的问题。写个小脚本,将两份日志进行合并,提升分析效率。(不是自动化分析=0=,刚工作的小白,分享学习下,写的估计很菜==)

 

功能:

输入log路径,输入问题时间点,输入搜索范围   ->  输出问题时间点,在搜索范围内的log,并将其按时间顺序进行合并。

 

效果图:

 技术分享图片

技术分享图片

 

源码:https://github.com/CzaOrz/smallStorage/tree/master/log-parse

 

核心代码如下:

import os,sys,time,datetime

appLog = {}        # 用于存储log文件信息
kmsgLog = {}    # key为时间,value为文件名
TIMEFORMAT = %m-%d %H:%M:%S.%f
# 测试专用 #resultPath = os.path.join(os.path.split(os.getcwd())[0], result.cza)
resultPath = os.path.join(os.getcwd(),result.cza)
def restoreFile(filename): # 根据log内容做定制,取特定时间信息 start
= filename.find(.) fileTime = filename[start+1:start+9] timeFormat = %Y%m%d time = datetime.datetime.strptime(fileTime,timeFormat) if filename.find(app-log) >= 0: appLog[time] = filename elif filename.find(kmsg-log) >= 0: kmsgLog[time] = filename else: print(there exist one error!) sys.exit() def mergeApp(testPath, problemTime, searchRange): # 合并app-log appList = [] # 以列表形式存储log内容 for index,filename in sorted(appLog.items(), key=lambda x: x[0], reverse=False): filename = os.path.join(testPath, filename) with open(filename,r) as fr: context = fr.readlines() appList.append(context) with open(resultPath,w) as fw: for context in appList: # 此处内容为列表 --BUG? for temp in context: # 故二次遍历 end = temp.find( , temp.find(.)) lineTime = temp[:end] # 7是定制时定义好的距离 if (datetime.datetime.strptime(lineTime,TIMEFORMAT) - problemTime).seconds <= int(searchRange): temp = app**** + temp fw.write(temp) else: pass def mergeKmsg(testPath, problemTime, searchRange): # 合并kmsg-log kmsgList = [] for index,filename in sorted(kmsgLog.items(), key=lambda x: x[0], reverse=False): filename = os.path.join(testPath, filename) with open(filename,r) as fr: context = fr.readlines() kmsgList.append(context) with open(resultPath,a) as fw: for context in kmsgList: for temp in context: end = temp.find( , temp.find(.)) lineTime = temp[:end] # 7是定制时定义好的距离 if (datetime.datetime.strptime(lineTime,TIMEFORMAT) - problemTime).seconds <= int(searchRange): temp = kmsg*** + temp fw.write(temp) else: pass def merge(): # 将app-log与kmsg-log混合合并,以时间排序 logDict = {} with open(resultPath,r) as fr: content = fr.readlines() with open(resultPath,w) as fw: for line in content: end = line.find( , line.find(.)) # 找出完成的时间段 time = line[7:end] time = datetime.datetime.strptime(time, TIMEFORMAT) logDict[time] = line for key,line in sorted(logDict.items(), key=lambda x:x[0], reverse=False): fw.write(line) print(merge done!) def mergeLog(testPath, problemTime, searchRange): # API,合并函数入口 ap_count,kmsg_count = 0,0 for DIR,files,filenameList in os.walk(testPath): for filename in filenameList: if filename.find(app-log) >= 0: restoreFile(filename) ap_count += 1 elif filename.find(kmsg-log) >= 0: restoreFile(filename) kmsg_count += 1 else: pass # 判断字典内容是否为空,为空则表示物对应目标文件 if appLog and kmsgLog is not None: print(restore done) else: print(not find aim log file) sys.exit() # 合并log文件 problemTime = datetime.datetime.strptime(problemTime, %m-%d %H:%M:%S) try: mergeApp(testPath,problemTime,searchRange) # 合并app-log mergeKmsg(testPath,problemTime,searchRange) # 合并kmsg-log merge() # 针对ap,kmsg,进行混合合并,按时间顺序排序 except: print(merge error) sys.exit() return ap_count,kmsg_count # 内部测试 if __name__ == __main__: testPath = os.path.join(os.path.split(os.getcwd())[0], test) problemTime = 12-30 14:58:0 searchRange = 300 # 5min is 300s mergeLog(testPath, problemTime, searchRange)

 

log合并小工具

标签:image   http   read   EAP   inf   return   字典   exist   text   

原文地址:https://www.cnblogs.com/czaOrz/p/10206677.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!