This repository was archived by the owner on Dec 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcombine_logs.py
executable file
·67 lines (54 loc) · 1.99 KB
/
combine_logs.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
63
64
65
66
67
#!/usr/bin/env python3
import os
LOG_DIR = "logging"
COMBINED_LOG = LOG_DIR + "/combined.log"
def get_log_files():
logs = []
for file in os.listdir(LOG_DIR):
if file.endswith(".log"):
logs.append(LOG_DIR + "/" + file)
return logs
def read_log_file(file_str):
file_dict = {}
f = open(file_str, 'r')
for line in f:
if not line.startswith('\t'):
# else we use the previous count, etc values
split = line.split(' ')
date = split[0]
time = split[1]
count = int(split[2].replace('[', '').replace(']', ''))
split2 = split[3].split('\t')
log_type = split2[0].replace(':', '')
log_level = split2[1]
message = ' '.join(split[4:]).replace('\n', '')
file_dict[count] = {
"date": date,
"time": time,
"log_type": log_type,
"log_level": log_level,
"message": message
}
else:
# If begin of new log, values have been set again, else
# if newline just using previous values and appending to message
message = line.replace('\t', '').replace('\n', '')
file_dict[count]["message"] += " %s" % message
return file_dict
def get_combined_log():
combined_dict = {}
for file in get_log_files():
combined_dict.update((read_log_file(file)))
sorted_keys = sorted(combined_dict.keys())
sorted_dict = {}
for key in sorted_keys:
sorted_dict[key] = combined_dict[key]
return sorted_dict
if __name__ == "__main__":
combined_log = get_combined_log()
f = open(COMBINED_LOG, 'w')
for index in combined_log.keys():
log_data = combined_log[index]
line = "%s %s [%s] %s:\t%s %s" % (log_data["date"], log_data["time"], index, log_data["log_type"], log_data["log_level"], log_data["message"])
f.write(line + '\n')
f.close()