Refactor logger

This commit is contained in:
Georges-Antoine Assi
2023-07-31 16:46:35 -04:00
parent ce7684ce6a
commit cc98b36fe0
3 changed files with 33 additions and 23 deletions

View File

@@ -1,37 +1,30 @@
import logging
COLORS: dict = {
'grey': '\033[92m',
'pink': '\033[95m',
'blue': '\033[94m',
'cyan': '\033[96m',
'orange': '\033[93m',
'orange_i': '\033[3;93m',
'red': '\033[91m',
'bold_red': '\033[1;91m',
'reset': '\033[0m',
"grey": "\033[92m",
"pink": "\033[95m",
"blue": "\033[94m",
"cyan": "\033[96m",
"orange": "\033[93m",
"orange_i": "\033[3;93m",
"red": "\033[91m",
"bold_red": "\033[1;91m",
"reset": "\033[0m",
}
class CustomFormatter(logging.Formatter):
level: str = "%(levelname)s"
dots: str = ":"
identifier: str = "\t [RomM]"
identifier_warning: str = " [RomM]"
identifier_critical: str = " [RomM]"
msg: str = "%(message)s"
date: str = "[%(asctime)s] "
FORMATS: dict = {
logging.DEBUG: COLORS['pink'] + level + COLORS['reset'] + dots + COLORS['blue'] + identifier + COLORS['cyan'] + date + COLORS['reset'] + msg,
logging.INFO: COLORS['grey'] + level + COLORS['reset'] + dots + COLORS['blue'] + identifier + COLORS['cyan'] + date + COLORS['reset'] + msg,
logging.WARNING: COLORS['orange'] + level + COLORS['reset'] + dots + COLORS['blue'] + identifier_warning + COLORS['cyan'] + date + COLORS['reset'] + msg,
logging.ERROR: COLORS['red'] + level + COLORS['reset'] + dots + COLORS['blue'] + identifier + COLORS['cyan'] + date + COLORS['reset'] + msg,
logging.CRITICAL: COLORS['bold_red'] + level + COLORS['reset'] + dots + COLORS['blue'] + identifier_critical + COLORS['cyan'] + date + COLORS['reset'] + msg
logging.DEBUG: f'{COLORS["pink"]}%(levelname)s{COLORS["reset"]}:{COLORS["blue"]}\t [RomM]{COLORS["cyan"]}[%(asctime)s]{COLORS["reset"]} %(message)s',
logging.INFO: f'{COLORS["grey"]}%(levelname)s{COLORS["reset"]}:{COLORS["blue"]}\t [RomM]{COLORS["cyan"]}[%(asctime)s]{COLORS["reset"]} %(message)s',
logging.WARNING: f'{COLORS["orange"]}%(levelname)s{COLORS["reset"]}:{COLORS["blue"]} [RomM]{COLORS["cyan"]}[%(asctime)s]{COLORS["reset"]} %(message)s',
logging.ERROR: f'{COLORS["red"]}%(levelname)s{COLORS["reset"]}:{COLORS["blue"]}\t [RomM]{COLORS["cyan"]}[%(asctime)s]{COLORS["reset"]} %(message)s',
logging.CRITICAL: f'{COLORS["bold_red"]}%(levelname)s{COLORS["reset"]}:{COLORS["blue"]} [RomM]{COLORS["cyan"]}[%(asctime)s]{COLORS["reset"]} %(message)s',
}
def format(self, record):
log_fmt = self.FORMATS.get(record.levelno)
formatter = logging.Formatter(fmt=log_fmt, datefmt='%Y-%m-%d %H:%M:%S')
formatter = logging.Formatter(fmt=log_fmt, datefmt="%Y-%m-%d %H:%M:%S")
return formatter.format(record)

View File

View File

@@ -0,0 +1,17 @@
from logger.logger import log
def test_logger(caplog):
log.debug("Testing debug message")
assert "debug message" in caplog.text
log.info("Testing info message")
assert "info message" in caplog.text
log.warning("Testing warning message")
assert "warning message" in caplog.text
log.error("Testing error message")
assert "error message" in caplog.text
log.critical("Testing critical message")
assert "critical message" in caplog.text