python实现扫描日志关键字的示例
作者:大繁至简 发布时间:2023-10-20 15:44:02
标签:python,关键字,扫描,日志
我们在压力测试过程会收集到很多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


猜你喜欢
- Windows操作系统支持Python的Python2版本和Python3版本,下载安装时要根据windows的操作系统来选择对应的Pyth
- 最近做的都是前端的项目,很多项目都有回到顶部的需求,下面把我写js代码做个笔录,方便以后查找。发现还可以添加从快到慢的动画效果和随时下拉滚动
- asp如何实现当前月份距离以前某个时间的月份数 如今天是2011年1月份,我想知道离2010年3月,计算这中间一共是几个月 最佳答案 <
- 安装pyqt5wind@wind-ThinkPad-X250:~/Downloads/PyQt5_gpl-5.12.2$ python3 -
- asp分页,是学习使用asp编程经常遇到的问题,也算是一个经典的问题。本文介绍了一个asp分页源代码例子,希望对初学者有所帮助,本程序文件名
- 首先看一下这三个函数:rtrim() ltrim() trim();rtrim()定义以及用法: rtrim() 函数移除字符串右侧的空白字
- pycharm的pytest功能在新建一个python文件时,比如名称是test_test_test.py,由于含有test,pycharm
- INSERT、DELETE、UPDATE 三种SQL语句是数据库技术的三大基本语句. 在通常的web开发中对它的处理可以说是无处不在. 如果
- XML即可扩展标记语言(eXtensible Markup Language)。标记是指计算机所能理解的信息符号,通过此种标记,计算机之间可
- 将Django与其他现有认证系统的用户名和密码或者认证方法进行整合是可以办到的。例如,你所在的公司也许已经安装了LDAP,并且为每一个员工都
- pip源配置文件可以放置的位置:Linux/Unix:/etc/pip.con~/.pip/pip.conf (每一个我都找了都没有,所以我
- 一、生成器1、生成器定义在Python中,一边循环一边计算的机制,称为生成器:generator2、生成器存在的意义列表所有数据都在内存中,
- #!c:\python27\python.exe# -*- coding: utf-8 -*-import osimport refrom
- 本文实例为大家分享了如何利用Python对数据库的增删改查进行简单的封装,供大家参考,具体内容如下1.insert &
- 1、pivot函数的定义pivot(index=None,columns=None,values=None) -> DataFrame
- 示例1我们将要请求五个不同的url:单线程import timeimport urllib2defget_responses(): &nbs
- 本文实例讲述了Python中bisect的用法,是一个比较常见的实用技巧。分享给大家供大家参考。具体分析如下:一般来说,Python中的bi
- Matplotlib可以无缝的处理LaTex字体,在图中加入数学公式from matplotlib.patches import Polyg
- 实例源码:#pip3 install opencv-pythonimport cv2from datetime import datetim
- 通过PyQt5实现设置一个小闹钟的功能,到了设置的时间后可以响起一段音乐来提醒。导入UI界面组件相关的模块from PyQt5.QtCore