-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger_config.py
45 lines (35 loc) · 1.39 KB
/
logger_config.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
# logger_config.py
import logging
from termcolor import colored
class ColoredFormatter(logging.Formatter):
LEVEL_COLORS = {
'INFO': 'yellow',
'DEBUG': 'grey',
'WARNING': 'magenta',
'ERROR': 'red',
'CRITICAL': 'red',
}
LINE_COLORS = {
'INFO': 'green',
'DEBUG': 'green',
'WARNING': 'green',
'ERROR': 'green',
'CRITICAL': 'green',
}
FILENAME_COLOR = 'yellow'
def format(self, record):
log_message = super().format(record)
line_color = self.LINE_COLORS.get(record.levelname, 'magenta')
colored_message = colored(log_message, line_color)
levelname_color = self.LEVEL_COLORS.get(record.levelname, 'magenta')
colored_levelname = colored(record.levelname, levelname_color, attrs=['bold'])
colored_filename = colored(record.filename, self.FILENAME_COLOR, attrs=['bold'])
colored_message = colored_message.replace(record.levelname, colored_levelname)
return colored_message.replace(record.filename, colored_filename)
# Create and configure logger
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
handler = logging.StreamHandler()
formatter = ColoredFormatter('%(asctime)s - [%(levelname)s] - [%(filename)s] - %(message)s', datefmt='%Y-%m-%d %H:%M:%S')
handler.setFormatter(formatter)
logger.addHandler(handler)