mirror of
https://github.com/AUTOMATIC1111/stable-diffusion-webui.git
synced 2025-04-06 04:39:01 +08:00
Merge pull request #14538 from akx/log-wut
Fix logging configuration again
This commit is contained in:
commit
a06dab8d7a
@ -1,41 +1,57 @@
|
|||||||
import os
|
|
||||||
import logging
|
import logging
|
||||||
|
import os
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from tqdm.auto import tqdm
|
from tqdm import tqdm
|
||||||
|
|
||||||
|
|
||||||
class TqdmLoggingHandler(logging.Handler):
|
class TqdmLoggingHandler(logging.Handler):
|
||||||
def __init__(self, level=logging.INFO):
|
def __init__(self, fallback_handler: logging.Handler):
|
||||||
super().__init__(level)
|
super().__init__()
|
||||||
|
self.fallback_handler = fallback_handler
|
||||||
|
|
||||||
def emit(self, record):
|
def emit(self, record):
|
||||||
try:
|
try:
|
||||||
msg = self.format(record)
|
# If there are active tqdm progress bars,
|
||||||
tqdm.write(msg)
|
# attempt to not interfere with them.
|
||||||
self.flush()
|
if tqdm._instances:
|
||||||
|
tqdm.write(self.format(record))
|
||||||
|
else:
|
||||||
|
self.fallback_handler.emit(record)
|
||||||
except Exception:
|
except Exception:
|
||||||
self.handleError(record)
|
self.fallback_handler.emit(record)
|
||||||
|
|
||||||
TQDM_IMPORTED = True
|
|
||||||
except ImportError:
|
except ImportError:
|
||||||
# tqdm does not exist before first launch
|
TqdmLoggingHandler = None
|
||||||
# I will import once the UI finishes seting up the enviroment and reloads.
|
|
||||||
TQDM_IMPORTED = False
|
|
||||||
|
|
||||||
def setup_logging(loglevel):
|
def setup_logging(loglevel):
|
||||||
if loglevel is None:
|
if loglevel is None:
|
||||||
loglevel = os.environ.get("SD_WEBUI_LOG_LEVEL")
|
loglevel = os.environ.get("SD_WEBUI_LOG_LEVEL")
|
||||||
|
|
||||||
loghandlers = []
|
if not loglevel:
|
||||||
|
return
|
||||||
|
|
||||||
if TQDM_IMPORTED:
|
if logging.root.handlers:
|
||||||
loghandlers.append(TqdmLoggingHandler())
|
# Already configured, do not interfere
|
||||||
|
return
|
||||||
|
|
||||||
if loglevel:
|
if os.environ.get("SD_WEBUI_RICH_LOG"):
|
||||||
log_level = getattr(logging, loglevel.upper(), None) or logging.INFO
|
from rich.logging import RichHandler
|
||||||
logging.basicConfig(
|
handler = RichHandler()
|
||||||
level=log_level,
|
else:
|
||||||
format='%(asctime)s %(levelname)s [%(name)s] %(message)s',
|
handler = logging.StreamHandler()
|
||||||
datefmt='%Y-%m-%d %H:%M:%S',
|
|
||||||
handlers=loghandlers
|
if TqdmLoggingHandler:
|
||||||
)
|
handler = TqdmLoggingHandler(handler)
|
||||||
|
|
||||||
|
formatter = logging.Formatter(
|
||||||
|
'%(asctime)s %(levelname)s [%(name)s] %(message)s',
|
||||||
|
'%Y-%m-%d %H:%M:%S',
|
||||||
|
)
|
||||||
|
|
||||||
|
handler.setFormatter(formatter)
|
||||||
|
|
||||||
|
log_level = getattr(logging, loglevel.upper(), None) or logging.INFO
|
||||||
|
logging.root.setLevel(log_level)
|
||||||
|
logging.root.addHandler(handler)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user