-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog_module.py
92 lines (74 loc) · 2.38 KB
/
log_module.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/awk BEGIN{a=ARGV[1];sub(/[a-z_.]+$/,".venv/bin/python",a);system(a"\t"ARGV[1])}
# -*- coding: utf-8 -*-
import logging
import logging.handlers
from datetime import datetime
from pathlib import Path
'''
logging elements
namespace
pod name
state(running)
kill policy
kill reason
* DEBUG level
all pod information
* INFO level
pod killing information
* WARMING level
pod killing
* ERROR level
# Two log handler
1. DEBUG level
2. INFO level
'''
def close_handler(logger):
for handler in logger.handlers:
logger.removeHandler(handler)
handler.close()
class log_module:
def __init__(self,file_path='./log'):
Path(file_path).mkdir(parents=True,exist_ok=True)
now_time = datetime.now().strftime('%Y-%m-%d')
self.logger = logging.getLogger('Kube_pod_manager')
self.logger.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
def create_handler(
filename,
formatter,
level,
maxBytes=10*1000*1024,
backupCount=10,
):
_logger = logging.handlers.RotatingFileHandler(
filename=f'{filename}',
mode='a',
maxBytes=maxBytes,
backupCount=backupCount,
)
_logger.setFormatter(formatter)
_logger.setLevel(level)
return _logger
debug_logger = create_handler(
filename=f'{file_path}/{now_time}_logging_debug.log',
formatter=formatter,
level = logging.DEBUG,
maxBytes=50*1000*1024,
)
info_logger = create_handler(
filename=f'{file_path}/{now_time}_logging_info.log',
formatter=formatter,
level = logging.INFO,
)
self.logger.addHandler(debug_logger)
self.logger.addHandler(info_logger)
self.logger.warning(f'SAVED_PATH:{file_path}/{now_time}_logging_*.log')
if __name__ == '__main__':
logger_module = log_module(file_path='./log')
logger = logger_module.logger
logger.debug('debug message')
logger.info('info message')
logger.warning('warn message')
logger.error('error message')
logger.critical('critical message')
close_handler(logger)