diff options
author | AUTOMATIC1111 <16777216c@gmail.com> | 2024-03-02 04:03:13 +0000 |
---|---|---|
committer | AUTOMATIC1111 <16777216c@gmail.com> | 2024-03-02 04:03:13 +0000 |
commit | bef51aed032c0aaa5cfd80445bc4cf0d85b408b5 (patch) | |
tree | 42957c454a4ac8d98488f19811b60359d05d88ba /modules/logging_config.py | |
parent | cf2772fab0af5573da775e7437e6acdca424f26e (diff) | |
parent | 13984857890401e8605a3e53bd671e900a18d73f (diff) | |
download | stable-diffusion-webui-gfx803-bef51aed032c0aaa5cfd80445bc4cf0d85b408b5.tar.gz stable-diffusion-webui-gfx803-bef51aed032c0aaa5cfd80445bc4cf0d85b408b5.tar.bz2 stable-diffusion-webui-gfx803-bef51aed032c0aaa5cfd80445bc4cf0d85b408b5.zip |
Merge branch 'release_candidate'
Diffstat (limited to 'modules/logging_config.py')
-rw-r--r-- | modules/logging_config.py | 63 |
1 files changed, 40 insertions, 23 deletions
diff --git a/modules/logging_config.py b/modules/logging_config.py index 79269875..8e31d8c9 100644 --- a/modules/logging_config.py +++ b/modules/logging_config.py @@ -1,41 +1,58 @@ -import os
import logging
+import os
try:
- from tqdm.auto import tqdm
+ from tqdm import tqdm
+
class TqdmLoggingHandler(logging.Handler):
- def __init__(self, level=logging.INFO):
- super().__init__(level)
+ def __init__(self, fallback_handler: logging.Handler):
+ super().__init__()
+ self.fallback_handler = fallback_handler
def emit(self, record):
try:
- msg = self.format(record)
- tqdm.write(msg)
- self.flush()
+ # If there are active tqdm progress bars,
+ # attempt to not interfere with them.
+ if tqdm._instances:
+ tqdm.write(self.format(record))
+ else:
+ self.fallback_handler.emit(record)
except Exception:
- self.handleError(record)
+ self.fallback_handler.emit(record)
- TQDM_IMPORTED = True
except ImportError:
- # tqdm does not exist before first launch
- # I will import once the UI finishes seting up the enviroment and reloads.
- TQDM_IMPORTED = False
+ TqdmLoggingHandler = None
+
def setup_logging(loglevel):
if loglevel is None:
loglevel = os.environ.get("SD_WEBUI_LOG_LEVEL")
- loghandlers = []
+ if not loglevel:
+ return
+
+ if logging.root.handlers:
+ # Already configured, do not interfere
+ return
+
+ formatter = logging.Formatter(
+ '%(asctime)s %(levelname)s [%(name)s] %(message)s',
+ '%Y-%m-%d %H:%M:%S',
+ )
+
+ if os.environ.get("SD_WEBUI_RICH_LOG"):
+ from rich.logging import RichHandler
+ handler = RichHandler()
+ else:
+ handler = logging.StreamHandler()
+ handler.setFormatter(formatter)
+
+ if TqdmLoggingHandler:
+ handler = TqdmLoggingHandler(handler)
- if TQDM_IMPORTED:
- loghandlers.append(TqdmLoggingHandler())
+ handler.setFormatter(formatter)
- if loglevel:
- log_level = getattr(logging, loglevel.upper(), None) or logging.INFO
- logging.basicConfig(
- level=log_level,
- format='%(asctime)s %(levelname)s [%(name)s] %(message)s',
- datefmt='%Y-%m-%d %H:%M:%S',
- handlers=loghandlers
- )
+ log_level = getattr(logging, loglevel.upper(), None) or logging.INFO
+ logging.root.setLevel(log_level)
+ logging.root.addHandler(handler)
|