diff options
author | 2021-09-07 00:32:12 +0200 | |
---|---|---|
committer | 2021-09-07 16:58:35 +0200 | |
commit | ecfe6bc1b89632c1161f5aad83a08a8459f8f13a (patch) | |
tree | 809dfe31afb8fbaa8a9db86da2393da597eaa9bc | |
parent | Remove basicConfig configuration (diff) |
Add selective trace loggers
Add selective enabling of trace loggers as described in python-discord/bot#1529
-rw-r--r-- | bot/constants.py | 1 | ||||
-rw-r--r-- | bot/log.py | 33 |
2 files changed, 32 insertions, 2 deletions
diff --git a/bot/constants.py b/bot/constants.py index 2313bfdb..ae6e02c1 100644 --- a/bot/constants.py +++ b/bot/constants.py @@ -138,6 +138,7 @@ class Client(NamedTuple): github_bot_repo = "https://github.com/python-discord/sir-lancebot" # Override seasonal locks: 1 (January) to 12 (December) month_override = int(environ["MONTH_OVERRIDE"]) if "MONTH_OVERRIDE" in environ else None + trace_loggers = environ.get("BOT_TRACE_LOGGERS") class Colours: @@ -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) |