2
2
#-*- coding: UTF-8 -*-
3
3
4
4
import re
5
+ import sys
5
6
import argparse
6
7
from collections import Counter
7
8
@@ -14,25 +15,26 @@ def log_slice(args,slow_log_handler):
14
15
_endtime = args .endtime .encode (_charset )
15
16
#迭代整个文件查找目标区间中的内容
16
17
try :
18
+ output_file = open (args .output_file ,'bw' )
17
19
for line in slow_log_handler :
18
20
#如果_find被标记为True,说明当前line正位于目标区间
19
21
if _finde == True :
20
22
#说明当前line位于目标区间,直接打印当前line,并判断是否已经到了退出的位置
21
23
# 有可能遇到 latin1 都解码不了的情况
22
- print ( line . decode ( _charset ), end = '' )
24
+ output_file . write ( line )
23
25
if _endtime in line :
24
26
break
25
27
continue
26
28
#判断是否已经进行目标区间
27
29
if _finde == False :
28
30
if _starttime in line :
29
31
_finde = True
30
- print ( line . decode ( _charset ), end = '' )
32
+ output_file . write ( line )
31
33
except Exception as err :
32
- if _charset == 'latin1' :
33
- pass
34
- else :
35
- raise err
34
+ print ( err )
35
+ sys . exit ()
36
+ finally :
37
+ output_file . close ()
36
38
37
39
38
40
def hot_table (args ,slow_log_handler ):
@@ -105,6 +107,7 @@ operations = {
105
107
if __name__ == "__main__" :
106
108
parser = argparse .ArgumentParser ()
107
109
parser .add_argument ('--slow-log-file' ,help = 'slow log file absolute path' )
110
+ parser .add_argument ('--output-file' ,default = '/tmp/s.log' ,help = 'output file' )
108
111
parser .add_argument ('--starttime' ,help = 'slow log start time flag' )
109
112
parser .add_argument ('--endtime' ,help = 'slow log end time flag' )
110
113
parser .add_argument ('--charset' ,default = 'latin1' )
0 commit comments