-
Notifications
You must be signed in to change notification settings - Fork 33
/
logger.py
25 lines (22 loc) · 864 Bytes
/
logger.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
import logging
import os
def get_logger(save_path):
if not os.path.exists(save_path):
os.makedirs(save_path)
txt_path = os.path.join(save_path, 'log.txt')
# logger
root_logger = logging.getLogger()
for handler in root_logger.handlers[:]:
root_logger.removeHandler(handler)
root_logger.setLevel(logging.WARNING)
logger = logging.getLogger('test')
formatter = logging.Formatter('%(asctime)s.%(msecs)03d - %(levelname)s: %(message)s',
datefmt='%y-%m-%d %H:%M:%S')
logger.setLevel(logging.INFO)
file_handler = logging.FileHandler(txt_path, mode='a')
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
console_handler = logging.StreamHandler()
console_handler.setFormatter(formatter)
logger.addHandler(console_handler)
return logger