forked from vorpal-research/kex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log_splitter.py
53 lines (41 loc) · 1.59 KB
/
log_splitter.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import argparse
import os
import re
parser = argparse.ArgumentParser(
description='Split log file onto multiple files by given criteria')
parser.add_argument('logfile', type=str, help='log file to split')
parser.add_argument('--criteria', type=str, required=False, default="",
help='file split condition')
parser.add_argument('--remove-time', default=False, action='store_true',
help='remove timestamps from log file entries')
args = parser.parse_args()
log_entry = re.compile('\\d+-\\d+-\\d+ \\d+:\\d+:\\d+\\.\\d+(.*)')
def split_file_name(split):
filename, file_extension = os.path.splitext(args.logfile)
return f'{filename}_{split}{file_extension}'
def write_split(index, buffer):
split_name = split_file_name(index)
with open(split_name, 'w', encoding='utf-8') as split_file:
split_file.writelines(ln.rstrip() + os.linesep for ln in buffer)
def remove_time(line):
match = log_entry.match(line)
if match is None:
return line
return match.group(1)
def run():
with open(args.logfile, 'r', encoding='utf-8') as logfile:
line_buffer = []
split_index = 0
for line in logfile:
if args.remove_time:
line = remove_time(line)
if not args.criteria or args.criteria not in line:
line_buffer.append(line)
continue
write_split(split_index, line_buffer)
split_index += 1
line_buffer = [line]
if line_buffer:
write_split(split_index, line_buffer)
if __name__ == '__main__':
run()