log合併小工具
阿新 • • 發佈:2019-01-02
在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)