python实现扫描日志关键字的示例
作者:大繁至简 时间:2023-10-20 15:44:02
我们在压力测试过程会收集到很多log,怎样快速从中找到有用信息呢?让python脚本帮我们做这部分工作吧!
废话不说,上代码
环境: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)
关键字及被扫描的日志路径信息,记录到文件中,以逗号+空格隔开,如,“, ”日志路径信息放到最后。
格式如下:
anr, dalvikvm: Could not find class 'android.app.usage., panic, C:\Users\BHJ\logcat1.log
执行结果:
来源:https://blog.csdn.net/BHJ1119/article/details/78039889
标签:python,关键字,扫描,日志
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
ASP.NET数据库操作类实例
2024-01-12 21:01:07
GO语言求100以内的素数
2024-05-08 10:13:41
在VScode里面添加Python解释器的详细步骤
2021-12-04 16:03:11
![](https://img.aspxhome.com/file/2023/7/100817_0s.png)
使用Python3 编写简单信用卡管理程序
2021-01-17 20:57:59
python单向链表的基本实现与使用方法【定义、遍历、添加、删除、查找等】
2021-08-24 03:26:17
YUI学习笔记(1)
2009-01-12 18:06:00
基于python判断字符串括号是否闭合{}[]()
2022-03-25 15:58:45
Python牛刀小试密码爆破
2021-10-05 14:18:47
vue 解决兄弟组件、跨组件深层次的通信操作
2024-05-09 15:12:55
![](https://img.aspxhome.com/file/2023/1/126551_0s.jpg)
python requests模拟登陆github的实现方法
2022-10-05 21:37:48
![](https://img.aspxhome.com/file/2023/1/115101_0s.jpg)
Python装饰器限制函数运行时间超时则退出执行
2022-09-07 18:12:54
Python深入学习之装饰器
2021-09-25 15:00:38
MSSQL 生成日期列表代码
2024-01-26 23:58:01
Python实现PS图像抽象画风效果的方法
2022-10-25 14:08:42
![](https://img.aspxhome.com/file/2023/7/94697_0s.jpg)
(X)HTML中最多余的元素标签
2008-06-15 15:48:00
前端来看看 maxthon bugs
2008-09-23 18:35:00
javascript自定义加载loading效果
2024-04-27 15:23:14
![](https://img.aspxhome.com/file/2023/0/135500_0s.png)
Python中TypeError:unhashable type:'dict'错误的解决办法
2022-05-09 19:57:41
![](https://img.aspxhome.com/file/2023/0/101590_0s.png)
wxpython中自定义事件的实现与使用方法分析
2023-02-10 07:50:26
PHP警告Cannot use a scalar value as an array的解决方法
2023-11-14 20:43:04