feat: Logging Improvements

This commit is contained in:
blessedcoolant 2023-05-13 02:13:49 +12:00
parent e5b7dd63e9
commit d796ea7bec
2 changed files with 52 additions and 49 deletions

View File

@ -2,34 +2,37 @@
"""invokeai.util.logging """invokeai.util.logging
Logging class for InvokeAI that produces console messages that follow Logging class for InvokeAI that produces console messages
the conventions established in InvokeAI 1.X through 2.X.
Usage:
One way to use it:
from invokeai.backend.util.logging import InvokeAILogger from invokeai.backend.util.logging import InvokeAILogger
logger = InvokeAILogger.getLogger(__name__) logger = InvokeAILogger.getLogger(name='InvokeAI') // Initialization
logger.critical('this is critical') (or)
logger.error('this is an error') logger = InvokeAILogger.getLogger(__name__) // To use the filename
logger.warning('this is a warning')
logger.info('this is info') logger.critical('this is critical') // Critical Message
logger.debug('this is debugging') logger.error('this is an error') // Error Message
logger.warning('this is a warning') // Warning Message
logger.info('this is info') // Info Message
logger.debug('this is debugging') // Debug Message
Console messages: Console messages:
### this is critical [12-05-2023 20]::[InvokeAI]::CRITICAL --> This is an info message [In Bold Red]
*** this is an error *** [12-05-2023 20]::[InvokeAI]::ERROR --> This is an info message [In Red]
** this is a warning [12-05-2023 20]::[InvokeAI]::WARNING --> This is an info message [In Yellow]
>> this is info [12-05-2023 20]::[InvokeAI]::INFO --> This is an info message [In Grey]
| this is debugging [12-05-2023 20]::[InvokeAI]::DEBUG --> This is an info message [In Grey]
Another way: Alternate Method (in this case the logger name will be set to InvokeAI):
import invokeai.backend.util.logging as ialog import invokeai.backend.util.logging as IAILogger
ialogger.debug('this is a debugging message') IAILogger.debug('this is a debugging message')
""" """
import logging import logging
# module level functions # module level functions
def debug(msg, *args, **kwargs): def debug(msg, *args, **kwargs):
InvokeAILogger.getLogger().debug(msg, *args, **kwargs) InvokeAILogger.getLogger().debug(msg, *args, **kwargs)
@ -58,46 +61,43 @@ def basicConfig(**kwargs):
def getLogger(name: str = None) -> logging.Logger: def getLogger(name: str = None) -> logging.Logger:
return InvokeAILogger.getLogger(name) return InvokeAILogger.getLogger(name)
class InvokeAILogFormatter(logging.Formatter): class InvokeAILogFormatter(logging.Formatter):
''' '''
Repurposed from: Custom Formatting for the InvokeAI Logger
https://stackoverflow.com/questions/14844970/modifying-logging-message-format-based-on-message-logging-level-in-python3
''' '''
crit_fmt = "### %(msg)s"
err_fmt = "*** %(msg)s"
warn_fmt = "** %(msg)s"
info_fmt = ">> %(msg)s"
dbg_fmt = " | %(msg)s"
def __init__(self): # Color Codes
super().__init__(fmt="%(levelno)d: %(msg)s", datefmt=None, style='%') grey = "\x1b[38;20m"
yellow = "\x1b[33;20m"
red = "\x1b[31;20m"
bold_red = "\x1b[31;1m"
reset = "\x1b[0m"
# Log Format
format = "[%(asctime)s]::[%(name)s]::%(levelname)s --> %(message)s"
## More Formatting Options: %(pathname)s, %(filename)s, %(module)s, %(lineno)d
# Format Map
FORMATS = {
logging.DEBUG: grey + format + reset,
logging.INFO: grey + format + reset,
logging.WARNING: yellow + format + reset,
logging.ERROR: red + format + reset,
logging.CRITICAL: bold_red + format + reset
}
def format(self, record): def format(self, record):
# Remember the format used when the logging module log_fmt = self.FORMATS.get(record.levelno)
# was installed (in the event that this formatter is formatter = logging.Formatter(log_fmt, datefmt="%d-%m-%Y %H:%M:%S")
# used with the vanilla logging module. return formatter.format(record)
format_orig = self._style._fmt
if record.levelno == logging.DEBUG:
self._style._fmt = InvokeAILogFormatter.dbg_fmt
if record.levelno == logging.INFO:
self._style._fmt = InvokeAILogFormatter.info_fmt
if record.levelno == logging.WARNING:
self._style._fmt = InvokeAILogFormatter.warn_fmt
if record.levelno == logging.ERROR:
self._style._fmt = InvokeAILogFormatter.err_fmt
if record.levelno == logging.CRITICAL:
self._style._fmt = InvokeAILogFormatter.crit_fmt
# parent class does the work
result = super().format(record)
self._style._fmt = format_orig
return result
class InvokeAILogger(object): class InvokeAILogger(object):
loggers = dict() loggers = dict()
@classmethod @classmethod
def getLogger(self, name:str='invokeai')->logging.Logger: def getLogger(self, name: str = 'InvokeAI') -> logging.Logger:
if name not in self.loggers: if name not in self.loggers:
logger = logging.getLogger(name) logger = logging.getLogger(name)
logger.setLevel(logging.DEBUG) logger.setLevel(logging.DEBUG)

View File

@ -2,6 +2,9 @@
# Copyright (c) 2022 Kyle Schouviller (https://github.com/kyle0654) # Copyright (c) 2022 Kyle Schouviller (https://github.com/kyle0654)
import logging
logging.getLogger("xformers").addFilter(lambda record: 'A matching Triton is not available' not in record.getMessage())
import os import os
import sys import sys