forked from andreafortuna/CFPiHole
-
Notifications
You must be signed in to change notification settings - Fork 1
/
logger_config.py
38 lines (30 loc) · 1.24 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
import logging
class CustomFormatter(logging.Formatter):
COLOR_CODES = {
logging.DEBUG: "\x1b[38;20m", # Grey
logging.INFO: "\x1b[38;20m", # Grey (same as debug for consistency)
logging.WARNING: "\x1b[33;20m", # Yellow
logging.ERROR: "\x1b[31;20m", # Red
logging.CRITICAL: "\x1b[31;1m", # Bold Red
}
RESET = "\x1b[0m"
YELLOW = "\x1b[33;20m"
GREEN = "\x1b[33;92m"
FORMAT = "%(message)s"
def format(self, record):
color = self.COLOR_CODES.get(record.levelno, self.RESET)
log_fmt = f"{color}{self.FORMAT}{self.RESET}"
return logging.Formatter(log_fmt).format(record)
@staticmethod
def configure_logger(name: str, level=logging.INFO):
logger = logging.getLogger(name)
if not logger.hasHandlers(): # Avoid adding handlers multiple times
logger.setLevel(level)
# Create a console handler with custom formatter
console_handler = logging.StreamHandler()
console_handler.setLevel(level)
console_handler.setFormatter(CustomFormatter())
# Add the handler to the logger and disable propagation
logger.addHandler(console_handler)
logger.propagate = False
return logger