* Separated tests in multiple files. * Added pydantic validation for configuration. * Improved backup/restore on visit of nodes. * Echoing of variables with default no longer visits the default when not used (and no longer creates variables that were not actually used).
99 lines
3.5 KiB
Python
99 lines
3.5 KiB
Python
from enum import Enum
|
|
import logging
|
|
import sys
|
|
import copy
|
|
from ppp_classes import SUPPORTED_APPS
|
|
|
|
|
|
class DEBUG_LEVEL(Enum):
|
|
none = "none"
|
|
minimal = "minimal"
|
|
full = "full"
|
|
|
|
|
|
class PromptPostProcessorLogFactory: # pylint: disable=too-few-public-methods
|
|
"""
|
|
Factory class for creating loggers for the PromptPostProcessor module.
|
|
"""
|
|
|
|
class ColoredFormatter(logging.Formatter):
|
|
"""
|
|
A custom logging formatter that adds color to log records based on their level.
|
|
|
|
Attributes:
|
|
COLORS (dict): A dictionary mapping log levels to ANSI escape codes for colors.
|
|
|
|
Methods:
|
|
format(record): Formats the log record with color based on its level.
|
|
|
|
"""
|
|
|
|
COLORS = {
|
|
"DEBUG": "\033[0;36m", # CYAN
|
|
"INFO": "\033[0;32m", # GREEN
|
|
"WARNING": "\033[0;33m", # YELLOW
|
|
"ERROR": "\033[0;31m", # RED
|
|
"CRITICAL": "\033[0;37;41m", # WHITE ON RED
|
|
"RESET": "\033[0m", # RESET COLOR
|
|
}
|
|
|
|
def format(self, record):
|
|
"""
|
|
Formats the log record with color based on the log level.
|
|
|
|
Args:
|
|
record (LogRecord): The log record to be formatted.
|
|
|
|
Returns:
|
|
str: The formatted log record.
|
|
"""
|
|
colored_record = copy.copy(record)
|
|
levelname = colored_record.levelname
|
|
seq = self.COLORS.get(levelname, self.COLORS["RESET"])
|
|
colored_record.levelname = f"{seq}{levelname:8s}{self.COLORS['RESET']}"
|
|
return super().format(colored_record)
|
|
|
|
def __init__(self, app: SUPPORTED_APPS = None, filename = None): # pylint: disable=unused-argument
|
|
"""
|
|
Initializes the PromptPostProcessor class.
|
|
|
|
This method sets up the logger for the PromptPostProcessor class and configures its log level and handlers.
|
|
|
|
Args:
|
|
filename (str, optional): The name of the file to log to. Defaults to None.
|
|
app (SUPPORTED_APPS): The application for which the logger is being created.
|
|
|
|
Returns:
|
|
None
|
|
"""
|
|
ppplog = logging.getLogger("PromptPostProcessor")
|
|
ppplog.propagate = False
|
|
if not ppplog.handlers:
|
|
handler = logging.StreamHandler(sys.stdout)
|
|
handler.setFormatter(self.ColoredFormatter("%(asctime)s %(levelname)s %(message)s"))
|
|
ppplog.addHandler(handler)
|
|
if filename is not None:
|
|
file_handler = logging.FileHandler(filename, encoding="utf-8")
|
|
file_handler.setFormatter(logging.Formatter("%(asctime)s %(levelname)s %(message)s"))
|
|
ppplog.addHandler(file_handler)
|
|
ppplog.setLevel(logging.DEBUG)
|
|
self.log = PromptPostProcessorLogCustomAdapter(ppplog)
|
|
|
|
|
|
class PromptPostProcessorLogCustomAdapter(logging.LoggerAdapter):
|
|
"""
|
|
Custom logger adapter for the PromptPostProcessor.
|
|
This adapter adds a prefix to log messages to indicate that they are related to the PromptPostProcessor.
|
|
"""
|
|
|
|
def process(self, msg, kwargs):
|
|
"""
|
|
Process the log message and keyword arguments.
|
|
Args:
|
|
msg (str): The log message.
|
|
kwargs (dict): The keyword arguments.
|
|
Returns:
|
|
tuple: A tuple containing the processed log message and keyword arguments.
|
|
"""
|
|
return f"[PPP] {msg}", kwargs
|