From e8c1c81edf6b3ddffdec0771ad0fd6d02e6ef5ac Mon Sep 17 00:00:00 2001 From: Shreya Date: Tue, 11 May 2021 18:16:45 +0530 Subject: [PATCH] Move `DEFAULT_LOG_LEVEL` and add function `load_server_config` to monkey_island.py --- monkey/monkey_island.py | 30 ++++++++++++------- .../monkey_island/cc/server_utils/consts.py | 2 -- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/monkey/monkey_island.py b/monkey/monkey_island.py index 64d4b1f83..b0e510d21 100644 --- a/monkey/monkey_island.py +++ b/monkey/monkey_island.py @@ -7,9 +7,26 @@ gevent_monkey.patch_all() import json # noqa: E402 import os # noqa: E402 -from monkey_island.cc.server_utils.consts import DEFAULT_DATA_DIR, DEFAULT_LOG_LEVEL # noqa: E402 +from monkey_island.cc.server_utils.consts import DEFAULT_DATA_DIR # noqa: E402 from monkey_island.cc.server_utils.island_logger import setup_logging # noqa: E402 +DEFAULT_LOG_LEVEL = "INFO" + + +def load_server_config(path): + with open(server_config_path, "r") as f: + config_content = f.read() + data = json.loads(config_content) + data_dir = os.path.abspath( + os.path.expanduser( + os.path.expandvars(data["data_dir"] if "data_dir" in data else DEFAULT_DATA_DIR) + ) + ) + log_level = data["log_level"] if "log_level" in data else DEFAULT_LOG_LEVEL + + return data_dir, log_level + + if "__main__" == __name__: island_args = parse_cli_args() @@ -17,15 +34,8 @@ if "__main__" == __name__: # imports, so the log init needs to be first. try: server_config_path = os.path.expanduser(island_args.server_config) - with open(server_config_path, "r") as f: - config_content = f.read() - data = json.loads(config_content) - data_dir = os.path.abspath( - os.path.expanduser( - os.path.expandvars(data["data_dir"] if "data_dir" in data else DEFAULT_DATA_DIR) - ) - ) - log_level = data["log_level"] if "log_level" in data else DEFAULT_LOG_LEVEL + + data_dir, log_level = load_server_config(server_config_path) setup_logging(data_dir, log_level) diff --git a/monkey/monkey_island/cc/server_utils/consts.py b/monkey/monkey_island/cc/server_utils/consts.py index afb0d5c89..f0dba26dc 100644 --- a/monkey/monkey_island/cc/server_utils/consts.py +++ b/monkey/monkey_island/cc/server_utils/consts.py @@ -11,6 +11,4 @@ DEFAULT_DEVELOP_SERVER_CONFIG_PATH = os.path.join( MONKEY_ISLAND_ABS_PATH, "cc", "server_config.json.develop" ) -DEFAULT_LOG_LEVEL = "NOTSET" - DEFAULT_DATA_DIR = os.path.join(MONKEY_ISLAND_ABS_PATH, "cc")