-
Notifications
You must be signed in to change notification settings - Fork 0
/
init_logger.py
58 lines (44 loc) · 1.7 KB
/
init_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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import logging
from sys import stdout as display
FORMAT_DISPLAY = '%(levelname) -7s - %(message)s'
FORMAT_LOG_FILE = '%(asctime)s | %(name)s | %(levelname) -7s - %(message)s'
FORMAT_DATETIME = '%Y/%m/%d %H:%M:%S'
BASE_LOG_LEVEL = logging.DEBUG
DISPLAY_LEVEL = logging.DEBUG
FILE_LEVEL = logging.INFO
FILE_OUTPUT = 'output.log'
class CustomFormatter(logging.Formatter):
"""Logging Formatter to add colors and count warning / errors"""
NO_COLOR = "\x1b[m"
RED, GREEN, ORANGE, BLUE, PURPLE, L_BLUE, GREY, WHITE = \
map("\x1b[%dm".__mod__, range(31, 39))
BOLD_RED = '\x1b[31;1m'
format = FORMAT_DISPLAY
FORMATS = {
logging.DEBUG: BLUE + format + NO_COLOR,
logging.INFO: WHITE + format + NO_COLOR,
logging.WARNING: ORANGE + format + NO_COLOR,
logging.ERROR: RED + format + NO_COLOR,
logging.CRITICAL: BOLD_RED + format + NO_COLOR
}
def format(self, record):
log_fmt = self.FORMATS.get(record.levelno)
formatter = logging.Formatter(log_fmt)
return formatter.format(record)
def get_my_logger(mod_name):
logger = logging.getLogger(mod_name)
logger.setLevel(BASE_LOG_LEVEL)
formatter_file = logging.Formatter(fmt=FORMAT_LOG_FILE, datefmt=FORMAT_DATETIME)
# StreamHandler
stream_handler = logging.StreamHandler(display)
stream_handler.setLevel(DISPLAY_LEVEL)
stream_handler.setFormatter(CustomFormatter())
logger.addHandler(stream_handler)
# FileHandler
file_handler = logging.FileHandler(FILE_OUTPUT, encoding='utf-8')
file_handler.setLevel(FILE_LEVEL)
file_handler.setFormatter(formatter_file)
logger.addHandler(file_handler)
# Other Handlers
# ...
return logger