python實現(xiàn)掃描日志關(guān)鍵字的示例
更新時間:2018年04月28日 14:40:56 作者:大繁至簡
下面小編就為大家分享一篇python實現(xiàn)掃描日志關(guān)鍵字的示例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
我們在壓力測試過程會收集到很多l(xiāng)og,怎樣快速從中找到有用信息呢?讓python腳本幫我們做這部分工作吧!
廢話不說,上代碼
環(huán)境:win10 + python2.7.14
#-*- encoding: utf-8 -*-
#author : beihuijie
#version 1.1
import re
import sys
import os
import countTime
def getParameters():
'''
get parameters from console command
'''
with open(sys.argv[1], "r") as fread:
lines = fread.readlines()
keywords=[]
for line in lines:
temp = line.split(', ')
keywords.append(temp)
for i in range(0, (len(keywords[0]) - 1)):
print ' Keyword = %s' % keywords[0][i]
return keywords[0]
def isFileExists(strfile):
'''
check the file whether exists
'''
return os.path.isfile(strfile)
def Search(keyword, filename):
'''
search the keyword in a assign file
'''
if(isFileExists(filename) == False):
print 'Input filepath is wrong,please check again!'
sys.exit()
linenum = 1
findtime = 0
with open(filename, 'r') as fread:
lines = fread.readlines()
for line in lines:
rs = re.findall(keyword, line, re.IGNORECASE)
if rs:
#output linenum of keyword place
sys.stdout.write('line:%d '%linenum)
lsstr = line.split(keyword)
strlength = len(lsstr)
findtime = findtime + 1
#print strlength
for i in range(strlength):
if(i < (strlength - 1)):
sys.stdout.write(lsstr[i].strip())
sys.stdout.write(keyword)
else:
sys.stdout.write(lsstr[i].strip() + '\n')
linenum = linenum + 1
print '+----------------------------------------------------------------------------+'
print (' Search result: find keyword: %s %d times'%(keyword, findtime))
print '+----------------------------------------------------------------------------+'
def executeSearch():
'''
this is a execute search method
'''
ls = getParameters()
start = countTime.getTime()
parameter_number = len(ls)
print 'Filename = %s ' % ls[parameter_number - 1]
print '--------------------start search-------------------------'
if(parameter_number >= 2):
for i in range(parameter_number - 1):
Search(ls[i], ls[parameter_number - 1])
else:
print 'There is a parameter error occured in executeSearch()!'
end = countTime.getTime()
print '+----------------------------------------------------------------------------+'
print ' Total cost time: %s'%countTime.formatTime(end - start)
print '+============================================================================+'
if __name__=='__main__':
executeSearch()
countTime.py
#-*- encoding: utf-8 -*- #author : beihuijie #version 1.1 import datetime import time def getTime(): ''' return time is format of time(unit is second) ''' return time.time() def getCPUClockTime(): ''' return time is CPU Clock time ''' return time.clock() def formatTime(timevalue): ''' format the time numbers ''' hour = 0 minute = 0 second = 0 if timevalue > 0: #count hour hour = timevalue // 3600 remain = timevalue % 3600 #count minute minute = remain // 60 remain = remain % 60 #count second second = round(remain, 3) return '%.0fh:%.0fm:%.3fs'%(hour, minute, second) if __name__=='__main__': value = 134.45632 print value print formatTime(value)
關(guān)鍵字及被掃描的日志路徑信息,記錄到文件中,以逗號+空格隔開,如,“, ”日志路徑信息放到最后。
格式如下:
anr, dalvikvm: Could not find class 'android.app.usage., panic, C:\Users\BHJ\logcat1.log
執(zhí)行結(jié)果:

以上這篇python實現(xiàn)掃描日志關(guān)鍵字的示例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python繪圖之實現(xiàn)繪制極坐標(biāo)圖像
這篇文章主要介紹了如何利用python繪制極坐標(biāo)圖像,文中的示例代碼講解詳細(xì),具有一定的的參考價值,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-08-08
python opencv旋轉(zhuǎn)圖像(保持圖像不被裁減)
這篇文章主要為大家詳細(xì)介紹了python opencv旋轉(zhuǎn)圖像,保持圖像不被裁減,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-07-07

