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

 更新时间:2018年04月28日 14:40:56   作者:大繁至简  
下面小编就为大家分享一篇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

执行结果:

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

相关文章

  • python复制文件代码实现

    python复制文件代码实现

    最近在备份手机上的照片的时候,纯手工操作觉得有些麻烦,就想写个脚本自动进行。因为备份的时候有些照片以前备份过了,所以需要有个判重操作,看下面的实现代码
    2013-12-12
  • python3利用Dlib19.7实现人脸68个特征点标定

    python3利用Dlib19.7实现人脸68个特征点标定

    这篇文章主要为大家详细介绍了python3利用Dlib19.7实现人脸68个特征点标定,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-02-02
  • pytorch 使用半精度模型部署的操作

    pytorch 使用半精度模型部署的操作

    这篇文章主要介绍了pytorch 使用半精度模型部署的操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-05-05
  • python中similarity函数实例用法

    python中similarity函数实例用法

    在本篇文章里小编给大家整理的是一篇关于python中similarity函数实例用法,有兴趣的朋友们可以跟着学习下。
    2021-10-10
  • 详解将Django部署到Centos7全攻略

    详解将Django部署到Centos7全攻略

    这篇文章主要介绍了详解将Django部署到Centos7全攻略,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-09-09
  • 详解django中视图函数的FBV和CBV

    详解django中视图函数的FBV和CBV

    FBV是指视图函数以普通函数的形式,CBV是指视图函数以类的方式,这篇文章主要介绍了django中视图函数的FBV和CBV,需要的朋友可以参考下
    2022-08-08
  • Linux RedHat下安装Python2.7开发环境

    Linux RedHat下安装Python2.7开发环境

    这篇文章主要为大家详细介绍了Linux RedHat下安装Python2.7、pip、ipython环境、eclipse和PyDev环境,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-05-05
  • matplotlib实现数据实时刷新的示例代码

    matplotlib实现数据实时刷新的示例代码

    这篇文章主要介绍了matplotlib实现数据实时刷新的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-01-01
  • python 逆向爬虫正确调用 JAR 加密逻辑

    python 逆向爬虫正确调用 JAR 加密逻辑

    这篇文章主要介绍了python 逆向爬虫正确调用 JAR 加密逻辑,帮助大家更好的理解和使用python,感兴趣的朋友可以了解下
    2021-01-01
  • Python matplotlib之折线图的各种样式与画法总结

    Python matplotlib之折线图的各种样式与画法总结

    matplotlib是Python中的一个第三方库,主要用于开发2D图表,以渐进式、交互式的方式实现数据可视化,可以更直观的呈现数据,使数据更具说服力,下面这篇文章主要给大家介绍了关于Python matplotlib之折线图的各种样式与画法的相关资料,需要的朋友可以参考下
    2022-12-12

最新评论