diff options
author | 2021-09-07 00:32:12 +0200 | |
---|---|---|
committer | 2021-09-07 16:58:35 +0200 | |
commit | ecfe6bc1b89632c1161f5aad83a08a8459f8f13a (patch) | |
tree | 809dfe31afb8fbaa8a9db86da2393da597eaa9bc /bot/log.py | |
parent | Remove basicConfig configuration (diff) |
Add selective trace loggers
Add selective enabling of trace loggers as described in python-discord/bot#1529
Diffstat (limited to 'bot/log.py')
-rw-r--r-- | bot/log.py | 33 |
1 files changed, 31 insertions, 2 deletions
@@ -30,7 +30,6 @@ def setup() -> None: file_handler.setFormatter(log_format) root_logger = logging.getLogger() - root_logger.setLevel(logging.TRACE if Client.debug else logging.INFO) root_logger.addHandler(file_handler) if "COLOREDLOGS_LEVEL_STYLES" not in os.environ: @@ -44,8 +43,9 @@ def setup() -> None: if "COLOREDLOGS_LOG_FORMAT" not in os.environ: coloredlogs.DEFAULT_LOG_FORMAT = format_string - coloredlogs.install(stream=sys.stdout) + coloredlogs.install(level=logging.TRACE, stream=sys.stdout) + root_logger.setLevel(logging.DEBUG if Client.debug else logging.INFO) # Silence irrelevant loggers logging.getLogger("discord").setLevel(logging.ERROR) logging.getLogger("websockets").setLevel(logging.ERROR) @@ -53,6 +53,8 @@ def setup() -> None: logging.getLogger("matplotlib").setLevel(logging.ERROR) logging.getLogger("async_rediscache").setLevel(logging.WARNING) + _set_trace_loggers() + root_logger.info("Logging initialization complete") @@ -65,3 +67,30 @@ def _monkeypatch_trace(self: logging.Logger, msg: str, *args, **kwargs) -> None: """ if self.isEnabledFor(logging.TRACE): self._log(logging.TRACE, msg, args, **kwargs) + + +def _set_trace_loggers() -> None: + """ + Set loggers to the trace level according to the value from the BOT_TRACE_LOGGERS env var. + + When the env var is a list of logger names delimited by a comma, + each of the listed loggers will be set to the trace level. + + If this list is prefixed with a "!", all of the loggers except the listed ones will be set to the trace level. + + Otherwise if the env var begins with a "*", + the root logger is set to the trace level and other contents are ignored. + """ + level_filter = Client.trace_loggers + if level_filter: + if level_filter.startswith("*"): + logging.getLogger().setLevel(logging.TRACE) + + elif level_filter.startswith("!"): + logging.getLogger().setLevel(logging.TRACE) + for logger_name in level_filter.strip("!,").split(","): + logging.getLogger(logger_name).setLevel(logging.DEBUG) + + else: + for logger_name in level_filter.strip(",").split(","): + logging.getLogger(logger_name).setLevel(logging.TRACE) |