-
Notifications
You must be signed in to change notification settings - Fork 0
/
mylog.py
31 lines (28 loc) · 1.1 KB
/
mylog.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
import logging
import datetime
class MyLog():
def __init__(self, name: str = "undefinedName", level: str = "WARNING") -> None:
now = datetime.datetime.now()
self.logger = logging.getLogger(name)
self.logger.setLevel(self.get_level(level))
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
stream_handler = logging.StreamHandler()
stream_handler.setFormatter(formatter)
self.logger.addHandler(stream_handler)
file_handler = logging.FileHandler(f"{name}_{now.strftime('%m%d')}.log", encoding='utf-8')
file_handler.setFormatter(formatter)
self.logger.addHandler(file_handler)
def get_level(self, level):
if "WARN" in level:
return logging.WARNING
elif "INFO" in level:
return logging.INFO
elif "ERROR" in level:
return logging.ERROR
elif "DEBUG" in level:
return logging.DEBUG
elif "CIRTICAL" in level:
return logging.CRITICAL
else:
print(f"what is {level}??")
return 0