From 40d364dabb3fcd2c43c7d10f35c5511837b3481a Mon Sep 17 00:00:00 2001 From: Jakob Cornell Date: Tue, 13 Sep 2022 23:56:09 -0500 Subject: [PATCH] Make stdout logging threshold configurable --- strikebot/docs/sample_config.ini | 5 +++++ strikebot/src/strikebot/__main__.py | 13 ++++++++++--- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/strikebot/docs/sample_config.ini b/strikebot/docs/sample_config.ini index 28e510e..7efebdd 100644 --- a/strikebot/docs/sample_config.ini +++ b/strikebot/docs/sample_config.ini @@ -9,6 +9,11 @@ auth IDs = 13 15 bot user = count_better enforcing = true + +# log messages at or above this severity to standard out; level names and numbers are supported (optional, default +# WARNING); see https://docs.python.org/3/library/logging.html#levels +log level = WARNING + thread ID = abc123 diff --git a/strikebot/src/strikebot/__main__.py b/strikebot/src/strikebot/__main__.py index ac490b6..e725eeb 100644 --- a/strikebot/src/strikebot/__main__.py +++ b/strikebot/src/strikebot/__main__.py @@ -121,6 +121,13 @@ api_pool_error_window = dt.timedelta(seconds = main_cfg.getfloat("API pool error auth_ids = set(map(int, main_cfg["auth IDs"].split())) bot_user = main_cfg["bot user"] enforcing = main_cfg.getboolean("enforcing") + +raw_log_level = main_cfg.get("log level", "WARNING") +try: + log_level = int(raw_log_level) +except ValueError: + log_level = raw_log_level + reorder_buffer_time = dt.timedelta(seconds = main_cfg.getfloat("reorder buffer time")) request_queue_limit = main_cfg.getint("request queue limit") thread_id = main_cfg["thread ID"] @@ -138,14 +145,14 @@ db_connect_params = {k: getters.get(k, db_cfg.get)(k) for k in db_cfg} logger = getLogger(__package__) -logger.setLevel(logging.DEBUG) +logger.setLevel(logging.NOTSET - 1) # NOTSET means inherit from parent; we use handlers to filter handler = StreamHandler(stdout) -handler.setLevel(logging.WARNING) +handler.setLevel(log_level) handler.setFormatter(logging.Formatter("{asctime:23}: {name:17} {levelname:8}: {message}", style = "{")) logger.addHandler(handler) -if _DEBUG_LOG_PATH: +if _DEBUG_LOG_PATH: # TODO remove this ad hoc setup debug_handler = FileHandler(_DEBUG_LOG_PATH, mode = "w") debug_handler.setLevel(logging.DEBUG) debug_handler.setFormatter(logging.Formatter("{asctime:23}: {name:17} {levelname:8}: {message}", style = "{")) -- 2.30.2