diff options
| -rw-r--r-- | bot/constants.py | 1 | ||||
| -rw-r--r-- | bot/log.py | 37 | ||||
| -rw-r--r-- | config-default.yml | 7 | 
3 files changed, 36 insertions, 9 deletions
| diff --git a/bot/constants.py b/bot/constants.py index cc3aa41a5..7f270e5bd 100644 --- a/bot/constants.py +++ b/bot/constants.py @@ -199,6 +199,7 @@ class Bot(metaclass=YAMLGetter):      prefix: str      sentry_dsn: Optional[str]      token: str +    trace_loggers: Optional[str]  class Redis(metaclass=YAMLGetter): diff --git a/bot/log.py b/bot/log.py index e92233a33..4e20c005e 100644 --- a/bot/log.py +++ b/bot/log.py @@ -20,7 +20,6 @@ def setup() -> None:      logging.addLevelName(TRACE_LEVEL, "TRACE")      Logger.trace = _monkeypatch_trace -    log_level = TRACE_LEVEL if constants.DEBUG_MODE else logging.INFO      format_string = "%(asctime)s | %(name)s | %(levelname)s | %(message)s"      log_format = logging.Formatter(format_string) @@ -30,7 +29,6 @@ def setup() -> None:      file_handler.setFormatter(log_format)      root_log = logging.getLogger() -    root_log.setLevel(log_level)      root_log.addHandler(file_handler)      if "COLOREDLOGS_LEVEL_STYLES" not in os.environ: @@ -44,11 +42,9 @@ def setup() -> None:      if "COLOREDLOGS_LOG_FORMAT" not in os.environ:          coloredlogs.DEFAULT_LOG_FORMAT = format_string -    if "COLOREDLOGS_LOG_LEVEL" not in os.environ: -        coloredlogs.DEFAULT_LOG_LEVEL = log_level - -    coloredlogs.install(logger=root_log, stream=sys.stdout) +    coloredlogs.install(level=logging.TRACE, logger=root_log, stream=sys.stdout) +    root_log.setLevel(logging.DEBUG if constants.DEBUG_MODE else logging.INFO)      logging.getLogger("discord").setLevel(logging.WARNING)      logging.getLogger("websockets").setLevel(logging.WARNING)      logging.getLogger("chardet").setLevel(logging.WARNING) @@ -57,6 +53,8 @@ def setup() -> None:      # Set back to the default of INFO even if asyncio's debug mode is enabled.      logging.getLogger("asyncio").setLevel(logging.INFO) +    _set_trace_loggers() +  def setup_sentry() -> None:      """Set up the Sentry logging integrations.""" @@ -86,3 +84,30 @@ def _monkeypatch_trace(self: logging.Logger, msg: str, *args, **kwargs) -> None:      """      if self.isEnabledFor(TRACE_LEVEL):          self._log(TRACE_LEVEL, 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 = constants.Bot.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) diff --git a/config-default.yml b/config-default.yml index b7c446889..2830fd109 100644 --- a/config-default.yml +++ b/config-default.yml @@ -1,7 +1,8 @@  bot: -    prefix:      "!" -    sentry_dsn:  !ENV "BOT_SENTRY_DSN" -    token:       !ENV "BOT_TOKEN" +    prefix:         "!" +    sentry_dsn:     !ENV "BOT_SENTRY_DSN" +    token:          !ENV "BOT_TOKEN" +    trace_loggers:  !ENV "BOT_TRACE_LOGGERS"      clean:          # Maximum number of messages to traverse for clean commands | 
