log日志信息统计与画图
程序员文章站
2022-05-12 20:17:45
...
os.listdir(path)
可以将路径下的文件地址都拿到
----------------------------------------------------------------------------------------------------------------------------
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
from pyecharts import Line
file_paths = []
def get_paths(dir_path):
dir_list = os.listdir(dir_path)
for path in dir_list:
full_path = os.path.join(dir_path, path)
if os.path.isdir(full_path):
get_paths(full_path)
else:
file_paths.append(full_path)
def filter_log(path, log_prefix):
log_name = os.path.basename(path)
return log_name.startswith(log_prefix)
# return True if log_name.startswith(log_prefix) else False
if __name__ == '__main__':
log_type_count = {}
get_paths('log')
print(file_paths)
file_paths = filter(lambda path: os.path.basename(path).startswith('ccb'), file_paths)
for file in file_paths:
with open(file, encoding='utf-8') as f:
for line in f:
line = line.strip()
date, log_name, log_type, log_info = line.split(" - ")
date = date[:10]
if date in log_type_count:
if log_type in log_type_count[date]:
log_type_count[date][log_type] += 1
else:
log_type_count[date][log_type] = 1
else:
log_type_count[date] = {}
log_type_count[date][log_type] = 1
print('总数: ', log_type_count)
attr = log_type_count.get('2018-08-07', None)
print('data:', attr)
x = []
v1 = []
v2 = []
for key, value in log_type_count.items():
print(key)
print(value)
x.append(key)
v1.append(value['INFO'])
v2.append(value['WARNING'])
# attr = ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
# v1 = [5, 20, 36, 10, 10, 100]
# v2 = [55, 60, 16, 20, 15, 80]
line = Line("折线图示例")
line.add("INFO", x, v1, mark_point=["average"])
line.add("WARNING", x, v2, is_smooth=True, mark_line=["max", "average"])
line.render()
运行结果: