Files
acorderob-sd-webui-prompt-p…/ppp_logging.py
T
Antonio Cordero Balcazar 8a960df0aa * Add support for wildcards.
* Better debug log.
* Set commands support lazy evaluation and content addition.
* Unified in one tree and processing phase.
* Supports Flux.
* Fix evaluation of string variable as truthy.
* Show invalid wildcards.
* Better detection of installed UI.
* Improved cache of results.
* Works as a ComfyUI node.
* Debug setting changed to debug level.
* Fixes in variable use.
* Added timing reporting.
* Detection of more model types.
* Installation script for requirements.
* Extension metadata for A1111.
* Lark grammar in separate file and cached.
* Improved conditions for if command.
* Avoid repeats in the processing of prompts.
* Fix multiple loading of settings.
2024-08-31 14:02:46 +02:00

93 lines
3.0 KiB
Python

from enum import Enum
import logging
import sys
import copy
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):
"""
Initializes the PromptPostProcessor class.
This method sets up the logger for the PromptPostProcessor class and configures its log level and handlers.
Args:
None
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")) # Used in A1111 / Forge / reForge / ComfyUI, but not in SD.Next
ppplog.addHandler(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