goober/modules/logger.py

29 lines
1 KiB
Python
Raw Permalink Normal View History

import logging
from modules.globalvars import *
2025-07-23 10:19:08 +03:00
class GooberFormatter(logging.Formatter):
2025-07-23 10:19:08 +03:00
def __init__(self, colors: bool = True): # Disable colors for TXT output
self.colors = colors
self._format = f"[ %(levelname)-8s ]: %(message)s {DEBUG} [%(asctime)s.%(msecs)03d] (%(filename)s:%(funcName)s) {RESET}"
self.FORMATS = {
logging.DEBUG: DEBUG + self._format + RESET,
2025-07-23 10:19:08 +03:00
logging.INFO: self._format.replace(
"%(levelname)-8s", f"{GREEN}%(levelname)-8s{RESET}"
),
logging.WARNING: YELLOW + self._format + RESET,
logging.ERROR: RED + self._format + RESET,
2025-07-23 10:19:08 +03:00
logging.CRITICAL: PURPLE + self._format + RESET,
}
def format(self, record: logging.LogRecord):
if self.colors:
2025-07-23 10:19:08 +03:00
log_fmt = self.FORMATS.get(record.levelno) # Add colors
else:
2025-07-23 10:19:08 +03:00
log_fmt = self._format # Just use the default format
formatter = logging.Formatter(log_fmt, datefmt="%m/%d/%y %H:%M:%S")
return formatter.format(record)