-
Notifications
You must be signed in to change notification settings - Fork 2
/
logger.py
27 lines (22 loc) · 859 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
26
27
import logging
import os
def set_logger(name='exp', filepath=None, level='INFO'):
formatter = logging.Formatter(
fmt="[%(asctime)s %(name)s] %(message)s",
datefmt="%y-%m-%d %H:%M:%S")
logger = logging.getLogger(name)
logger.setLevel(logging.getLevelName(level))
handler = logging.StreamHandler()
handler.setFormatter(formatter)
logger.addHandler(handler)
if filepath is not None:
if os.path.dirname(filepath) is not '':
if not os.path.isdir(os.path.dirname(filepath)):
os.makedirs(os.path.dirname(filepath))
file_handle = logging.FileHandler(filename=filepath, mode="a")
file_handle.set_name("file")
file_handle.setFormatter(formatter)
logger.addHandler(file_handle)
return logger
def get_logger(name):
return logging.getLogger(name)