服务器之家

服务器之家 > 正文

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

时间:2021-02-07 00:32     来源/作者:大繁至简

我们在压力测试过程会收集到很多log,怎样快速从中找到有用信息呢?让python脚本帮我们做这部分工作吧!

废话不说,上代码

环境:win10 + python2.7.14

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#-*- 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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#-*- 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)

关键字及被扫描日志路径信息,记录到文件中,以逗号+空格隔开,如,“, ”日志路径信息放到最后。

格式如下:

?
1
anr, dalvikvm: could not find class 'android.app.usage., panic, c:\users\bhj\logcat1.log

执行结果:

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

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

原文链接:https://blog.csdn.net/BHJ1119/article/details/78039889

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部