2023-04-19 00:49:00 +00:00
|
|
|
# Copyright (c) 2023 Lincoln D. Stein and The InvokeAI Development Team
|
|
|
|
|
2023-04-11 13:33:28 +00:00
|
|
|
"""invokeai.util.logging
|
|
|
|
|
2023-05-12 14:13:49 +00:00
|
|
|
Logging class for InvokeAI that produces console messages
|
2023-04-11 13:33:28 +00:00
|
|
|
|
2023-05-12 14:13:49 +00:00
|
|
|
Usage:
|
2023-04-11 13:33:28 +00:00
|
|
|
|
|
|
|
from invokeai.backend.util.logging import InvokeAILogger
|
|
|
|
|
2023-05-12 14:13:49 +00:00
|
|
|
logger = InvokeAILogger.getLogger(name='InvokeAI') // Initialization
|
|
|
|
(or)
|
|
|
|
logger = InvokeAILogger.getLogger(__name__) // To use the filename
|
|
|
|
|
|
|
|
logger.critical('this is critical') // Critical Message
|
|
|
|
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
|
2023-04-11 13:33:28 +00:00
|
|
|
|
|
|
|
Console messages:
|
2023-05-12 14:13:49 +00:00
|
|
|
[12-05-2023 20]::[InvokeAI]::CRITICAL --> This is an info message [In Bold Red]
|
|
|
|
[12-05-2023 20]::[InvokeAI]::ERROR --> This is an info message [In Red]
|
|
|
|
[12-05-2023 20]::[InvokeAI]::WARNING --> This is an info message [In Yellow]
|
|
|
|
[12-05-2023 20]::[InvokeAI]::INFO --> This is an info message [In Grey]
|
|
|
|
[12-05-2023 20]::[InvokeAI]::DEBUG --> This is an info message [In Grey]
|
|
|
|
|
|
|
|
Alternate Method (in this case the logger name will be set to InvokeAI):
|
|
|
|
import invokeai.backend.util.logging as IAILogger
|
|
|
|
IAILogger.debug('this is a debugging message')
|
2023-04-11 13:33:28 +00:00
|
|
|
"""
|
2023-05-12 14:13:49 +00:00
|
|
|
|
2023-04-11 13:33:28 +00:00
|
|
|
import logging
|
|
|
|
|
2023-05-12 14:13:49 +00:00
|
|
|
|
2023-04-11 14:46:38 +00:00
|
|
|
# module level functions
|
|
|
|
def debug(msg, *args, **kwargs):
|
|
|
|
InvokeAILogger.getLogger().debug(msg, *args, **kwargs)
|
2023-04-11 13:33:28 +00:00
|
|
|
|
2023-04-11 14:46:38 +00:00
|
|
|
def info(msg, *args, **kwargs):
|
|
|
|
InvokeAILogger.getLogger().info(msg, *args, **kwargs)
|
2023-04-11 13:33:28 +00:00
|
|
|
|
2023-04-11 14:46:38 +00:00
|
|
|
def warning(msg, *args, **kwargs):
|
|
|
|
InvokeAILogger.getLogger().warning(msg, *args, **kwargs)
|
2023-04-11 13:33:28 +00:00
|
|
|
|
2023-04-11 14:46:38 +00:00
|
|
|
def error(msg, *args, **kwargs):
|
|
|
|
InvokeAILogger.getLogger().error(msg, *args, **kwargs)
|
2023-05-12 14:13:49 +00:00
|
|
|
|
2023-04-11 14:46:38 +00:00
|
|
|
def critical(msg, *args, **kwargs):
|
|
|
|
InvokeAILogger.getLogger().critical(msg, *args, **kwargs)
|
|
|
|
|
|
|
|
def log(level, msg, *args, **kwargs):
|
|
|
|
InvokeAILogger.getLogger().log(level, msg, *args, **kwargs)
|
|
|
|
|
|
|
|
def disable(level=logging.CRITICAL):
|
|
|
|
InvokeAILogger.getLogger().disable(level)
|
2023-04-11 13:33:28 +00:00
|
|
|
|
2023-04-11 15:10:43 +00:00
|
|
|
def basicConfig(**kwargs):
|
|
|
|
InvokeAILogger.getLogger().basicConfig(**kwargs)
|
|
|
|
|
2023-05-12 14:13:49 +00:00
|
|
|
def getLogger(name: str = None) -> logging.Logger:
|
2023-04-11 16:23:13 +00:00
|
|
|
return InvokeAILogger.getLogger(name)
|
|
|
|
|
2023-05-12 14:13:49 +00:00
|
|
|
|
2023-04-11 13:33:28 +00:00
|
|
|
class InvokeAILogFormatter(logging.Formatter):
|
|
|
|
'''
|
2023-05-12 14:13:49 +00:00
|
|
|
Custom Formatting for the InvokeAI Logger
|
2023-04-11 13:33:28 +00:00
|
|
|
'''
|
|
|
|
|
2023-05-12 14:13:49 +00:00
|
|
|
# Color Codes
|
|
|
|
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
|
|
|
|
}
|
2023-04-11 13:33:28 +00:00
|
|
|
|
|
|
|
def format(self, record):
|
2023-05-12 14:13:49 +00:00
|
|
|
log_fmt = self.FORMATS.get(record.levelno)
|
|
|
|
formatter = logging.Formatter(log_fmt, datefmt="%d-%m-%Y %H:%M:%S")
|
|
|
|
return formatter.format(record)
|
|
|
|
|
2023-04-11 13:33:28 +00:00
|
|
|
|
|
|
|
class InvokeAILogger(object):
|
|
|
|
loggers = dict()
|
2023-05-12 14:13:49 +00:00
|
|
|
|
2023-04-11 13:33:28 +00:00
|
|
|
@classmethod
|
2023-05-12 14:13:49 +00:00
|
|
|
def getLogger(self, name: str = 'InvokeAI') -> logging.Logger:
|
2023-04-11 13:33:28 +00:00
|
|
|
if name not in self.loggers:
|
|
|
|
logger = logging.getLogger(name)
|
|
|
|
logger.setLevel(logging.DEBUG)
|
|
|
|
ch = logging.StreamHandler()
|
|
|
|
fmt = InvokeAILogFormatter()
|
|
|
|
ch.setFormatter(fmt)
|
|
|
|
logger.addHandler(ch)
|
|
|
|
self.loggers[name] = logger
|
|
|
|
return self.loggers[name]
|