-
Notifications
You must be signed in to change notification settings - Fork 1
/
ppp_logging.py
92 lines (73 loc) · 2.99 KB
/
ppp_logging.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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