forked from udacity/nd0821-c3-starter-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
32 lines (25 loc) · 1.02 KB
/
__init__.py
File metadata and controls
32 lines (25 loc) · 1.02 KB
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
# a user can manipulate/configure logger from project libraries like any other Python object
import logging
from logging import Formatter, NullHandler
from colorama import Fore, Style
COLORS = {"DEBUG": Fore.BLUE,
"INFO": Fore.BLACK,
"WARNING": Fore.YELLOW,
"ERROR": Fore.RED,
"CRITICAL": Fore.MAGENTA}
class CustomColoredFormatter(Formatter):
def __init__(self, *, format, use_color):
''' Initialise customized formatter class '''
Formatter.__init__(self, fmt=format)
self.use_color = use_color
def format(self, record):
''' Sets message colour according log level '''
msg = super().format(record)
if self.use_color:
levelname = record.levelname
if hasattr(record, "color"):
return f"{record.color}{msg}{Style.RESET_ALL}"
if levelname in COLORS:
return f"{COLORS[levelname]}{msg}{Style.RESET_ALL}"
return msg
logging.getLogger(__name__).addHandler(NullHandler())