-
Notifications
You must be signed in to change notification settings - Fork 13
/
splitLogs.py
executable file
·62 lines (53 loc) · 1.89 KB
/
splitLogs.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
54
55
56
57
58
59
60
61
62
#!/usr/bin/env python3
import argparse
from datetime import datetime, timedelta
import gzip
import os
parser = argparse.ArgumentParser(description='Script to split .weechatlog files by day')
parser.add_argument('-f', '--file',
help='file to split up',
type=str,
required=True)
parser.add_argument('-p', '--prefix',
help='prefix for output file',
type=str,
default='')
parser.add_argument('-s', '--suffix',
help='suffix for output file',
type=str,
default='')
parser.add_argument('-c', '--compress',
help='compress the resulting log files with gzip',
action='store_true')
args = parser.parse_args()
def writeLog(date, lines):
fname = "{}{}{}".format(args.prefix, date, args.suffix)
lines = sorted(lines, key=lambda l: l[:19]) # stable sort lines by timestamp
if args.compress:
ago = datetime.utcnow() - timedelta(days=30)
if os.path.exists(f"{fname}.gz") and date < ago.strftime("%Y-%m-%d"):
return
with gzip.open(f"{fname}.gz", mode='wt') as output:
print(f"# writing {date} ...")
output.writelines(lines)
else:
with open(fname, 'w') as output:
output.writelines(lines)
with open(args.file, 'r') as wlog:
oneLogFile = []
currentDate = ""
for line in wlog:
newDate = line.split(" ", 1)[0]
if currentDate == "":
currentDate = newDate
if newDate != currentDate:
# write file
writeLog(currentDate, oneLogFile)
oneLogFile = []
currentDate = newDate
pass
# append to day's log
oneLogFile.append(line)
# write out today's log
if oneLogFile:
writeLog(currentDate, oneLogFile)