python实现扫描日志关键字的示例


Posted in Python onApril 28, 2018

我们在压力测试过程会收集到很多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

执行结果:

python实现扫描日志关键字的示例

以上这篇python实现扫描日志关键字的示例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
王纯业的Python学习笔记 下载
Feb 10 Python
Python操作SQLite简明教程
Jul 10 Python
详解Python中的strftime()方法的使用
May 22 Python
Python语言描述连续子数组的最大和
Jan 04 Python
tensorflow 加载部分变量的实例讲解
Jul 27 Python
在Python中给Nan值更改为0的方法
Oct 30 Python
为什么Python中没有&quot;a++&quot;这种写法
Nov 27 Python
Django中信号signals的简单使用方法
Jul 04 Python
pycharm中显示CSS提示的知识点总结
Jul 29 Python
Python计算IV值的示例讲解
Feb 28 Python
Python日志logging模块功能与用法详解
Apr 09 Python
使用OpenCV获取图像某点的颜色值,并设置某点的颜色
Jun 02 Python
python socket网络编程之粘包问题详解
Apr 28 #Python
在Windows中设置Python环境变量的实例讲解
Apr 28 #Python
PyTorch快速搭建神经网络及其保存提取方法详解
Apr 28 #Python
对Python中type打开文件的方式介绍
Apr 28 #Python
PyTorch上搭建简单神经网络实现回归和分类的示例
Apr 28 #Python
TensorFlow实现非线性支持向量机的实现方法
Apr 28 #Python
python 通过logging写入日志到文件和控制台的实例
Apr 28 #Python
You might like
多文件上传的例子
2006/10/09 PHP
Google Voice 短信发送接口PHP开源版(2010.5更新)
2010/07/22 PHP
win7 64位系统 配置php最新版开发环境(php+Apache+mysql)
2014/08/15 PHP
PHP 实现判断用户是否手机访问
2015/01/21 PHP
PHP魔术方法的使用示例
2015/06/23 PHP
JavaScript 节点操作 以及DOMDocument属性和方法
2007/12/06 Javascript
JavaScript操作XML 使用百度RSS作为新闻源示例
2012/02/17 Javascript
自己写的兼容ie和ff的在线文本编辑器类似ewebeditor
2012/12/12 Javascript
JavaScript插件化开发教程(六)
2015/02/01 Javascript
Jquery的基本对象转换和文档加载用法实例
2015/02/25 Javascript
jquery+CSS3模拟Path2.0动画菜单效果代码
2015/08/31 Javascript
JS代码实现table数据分页效果
2016/05/26 Javascript
jquery树形菜单效果的简单实例
2016/06/06 Javascript
jQuery中delegate()方法的用法详解
2016/10/13 Javascript
javascript中的面向对象
2017/03/30 Javascript
jquery.form.js异步提交表单详解
2017/04/25 jQuery
Laravel整合Bootstrap 4的完整方案(推荐)
2018/01/25 Javascript
js实现图片放大并跟随鼠标移动特效
2019/01/18 Javascript
如何基于JavaScript判断图片是否加载完成
2019/12/28 Javascript
原生JS实现弹幕效果的简单操作指南
2020/11/10 Javascript
从零学python系列之数据处理编程实例(一)
2014/05/22 Python
Python import用法以及与from...import的区别
2015/05/28 Python
让Django支持Sql Server作后端数据库的方法
2018/05/29 Python
numpy:找到指定元素的索引示例
2019/11/26 Python
pandas 对group进行聚合的例子
2019/12/27 Python
谷歌浏览器小字体处理方案即12px以下字体
2013/12/17 HTML / CSS
美国Randolph太阳镜官网:美国制造的飞行员太阳镜和射击眼镜
2018/06/15 全球购物
大学生写自荐信的技巧
2014/01/08 职场文书
工作自我评价怎么写
2014/01/29 职场文书
安全生产网格化管理实施方案
2014/03/01 职场文书
2014学习优秀共产党员先进事迹材料思想汇报
2014/09/14 职场文书
大学生党员个人对照检查材料范文
2014/09/25 职场文书
工作收入证明模板
2014/10/10 职场文书
经理助理岗位职责
2015/02/02 职场文书
2016年“5.12”护士节慰问信
2015/11/30 职场文书
Oracle创建只读账号的详细步骤
2021/06/07 Oracle