2025-07-08 17:02:10 +03:00
|
|
|
import logging
|
|
|
|
from modules.globalvars import *
|
|
|
|
|
2025-07-23 10:19:08 +03:00
|
|
|
|
2025-07-08 17:02:10 +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
|
2025-07-08 17:02:10 +03:00
|
|
|
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}"
|
|
|
|
),
|
2025-07-08 17:02:10 +03:00
|
|
|
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,
|
2025-07-08 17:02:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2025-07-08 17:02:10 +03:00
|
|
|
else:
|
2025-07-23 10:19:08 +03:00
|
|
|
log_fmt = self._format # Just use the default format
|
|
|
|
|
2025-07-08 17:02:10 +03:00
|
|
|
formatter = logging.Formatter(log_fmt, datefmt="%m/%d/%y %H:%M:%S")
|
|
|
|
return formatter.format(record)
|