-
Notifications
You must be signed in to change notification settings - Fork 0
/
logging_setup.py
34 lines (25 loc) · 1.13 KB
/
logging_setup.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
import coloredlogs, logging
LEVEL_TO_STRING = {logging.DEBUG: "DEBUG", logging.INFO: "INFO",
logging.WARN: "WARN", logging.ERROR: "ERROR",
logging.CRITICAL: "CRITICAL"}
def setup_logger(name, log_filename=None, log_level=logging.DEBUG):
"""Setup a logger with the specified name and file.
If a log_filename is specified, the logger will also output logs to that
file-
"""
FORMAT = "%(asctime)s %(name)s[%(process)d] %(levelname)s %(message)s"
logger = logging.getLogger(name)
logger.setLevel(log_level)
coloredlogs.install(fmt=FORMAT, datefmt="%H:%M:%S",
level=LEVEL_TO_STRING[log_level], logger=logger)
if log_filename:
formatter = logging.Formatter(FORMAT)
handler = logging.FileHandler(log_filename)
handler.setFormatter(formatter)
logger.addHandler(handler)
return logger
# logger = setup_logger('first_logger', 'first_logfile.log')
#logger.info('This is just info message')
# second file logger
#super_logger = setup_logger('second_logger', 'second_logfile.log')
#super_logger.error('This is an error message')