-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog_analyzer.py
More file actions
29 lines (24 loc) · 909 Bytes
/
Copy pathlog_analyzer.py
File metadata and controls
29 lines (24 loc) · 909 Bytes
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
import json
class LogAnalyzer:
def __init__(self):
self.file_content = None
self.log_levels_count = {}
def read_log(self, file_path):
with open(file_path, 'r') as file:
self.file_content = file.readlines()
def print_content(self):
print(self.file_content)
def extract_count_from_log_file(self):
for line in self.file_content:
if line.startswith('['):
log_level = line.split(']')[0][1:]
self.log_levels_count[log_level] = self.log_levels_count.get(log_level, 0) + 1
def save_to_json(self, out_file='log_count.json'):
with open(out_file, 'w') as file:
json.dump(self.log_levels_count, file, indent=2)
if __name__ == '__main__':
la = LogAnalyzer()
la.read_log('server.log')
la.extract_count_from_log_file()
print(la.log_levels_count)
la.save_to_json()