-
Notifications
You must be signed in to change notification settings - Fork 50
/
logger.py
27 lines (21 loc) · 860 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 logging.config
import logging.handlers
from path import Path
def init_logger(level='INFO', log_dir='./', log_name='main_logger', filename='main.log'):
logger = logging.getLogger(log_name)
fh = logging.handlers.RotatingFileHandler(
Path(log_dir) / filename, 'w', 20 * 1024 * 1024, 5)
formatter = logging.Formatter('%(asctime)s %(levelname)5s - %(name)s '
'[%(filename)s line %(lineno)d] - %(message)s',
datefmt='%m-%d %H:%M:%S')
fh.setFormatter(formatter)
logger.addHandler(fh)
# logging to screen
fh = logging.StreamHandler()
formatter = logging.Formatter('[%(levelname)s] %(message)s',)
fh.setFormatter(formatter)
logger.addHandler(fh)
logger.setLevel(level)
logger.info("Start training")
return logger