From 785f2ef77da311e3893de13ee9a5022a7bd7b60b Mon Sep 17 00:00:00 2001 From: Shreya Date: Mon, 10 May 2021 13:49:33 +0530 Subject: [PATCH] Replace `json_setup_logging()` with `setup_logging()` to configure logger --- monkey/monkey_island.py | 4 +- .../cc/island_logger_default_config.json | 33 ---------- .../monkey_island/cc/server_utils/consts.py | 4 -- .../cc/server_utils/island_logger.py | 65 ++++++++++++------- 4 files changed, 45 insertions(+), 61 deletions(-) delete mode 100644 monkey/monkey_island/cc/island_logger_default_config.json diff --git a/monkey/monkey_island.py b/monkey/monkey_island.py index ca5b2639b..2d86603f5 100644 --- a/monkey/monkey_island.py +++ b/monkey/monkey_island.py @@ -10,7 +10,7 @@ from pathlib import Path # noqa: E402 import monkey_island.cc.environment.server_config_generator as server_config_generator # noqa: E402 from monkey_island.cc.server_utils.consts import DEFAULT_DATA_DIR, DEFAULT_LOG_LEVEL # noqa: E402 -from monkey_island.cc.server_utils.island_logger import json_setup_logging # noqa: E402 +from monkey_island.cc.server_utils.island_logger import setup_logging # noqa: E402 if "__main__" == __name__: island_args = parse_cli_args() @@ -32,7 +32,7 @@ if "__main__" == __name__: ) log_level = data["log_level"] if "log_level" in data else DEFAULT_LOG_LEVEL - # json_setup_logging(data_dir, log_level) + setup_logging(data_dir, log_level) except json.JSONDecodeError as ex: print(f"Error loading logging config: {ex}") diff --git a/monkey/monkey_island/cc/island_logger_default_config.json b/monkey/monkey_island/cc/island_logger_default_config.json deleted file mode 100644 index 522177cda..000000000 --- a/monkey/monkey_island/cc/island_logger_default_config.json +++ /dev/null @@ -1,33 +0,0 @@ -{ - "version": 1, - "disable_existing_loggers": false, - "formatters": { - "simple": { - "format": "%(asctime)s - %(filename)s:%(lineno)s - %(funcName)10s() - %(levelname)s - %(message)s" - } - }, - "handlers": { - "console": { - "class": "logging.StreamHandler", - "level": "DEBUG", - "formatter": "simple", - "stream": "ext://sys.stdout" - }, - "info_file_handler": { - "class": "logging.handlers.RotatingFileHandler", - "level": "INFO", - "formatter": "simple", - "filename": "info.log", - "maxBytes": 10485760, - "backupCount": 20, - "encoding": "utf8" - } - }, - "root": { - "level": "DEBUG", - "handlers": [ - "console", - "info_file_handler" - ] - } -} \ No newline at end of file diff --git a/monkey/monkey_island/cc/server_utils/consts.py b/monkey/monkey_island/cc/server_utils/consts.py index 41bd6e4a7..afb0d5c89 100644 --- a/monkey/monkey_island/cc/server_utils/consts.py +++ b/monkey/monkey_island/cc/server_utils/consts.py @@ -11,10 +11,6 @@ DEFAULT_DEVELOP_SERVER_CONFIG_PATH = os.path.join( MONKEY_ISLAND_ABS_PATH, "cc", "server_config.json.develop" ) -DEFAULT_LOGGER_CONFIG_PATH = os.path.join( - MONKEY_ISLAND_ABS_PATH, "cc", "island_logger_default_config.json" -) - DEFAULT_LOG_LEVEL = "NOTSET" DEFAULT_DATA_DIR = os.path.join(MONKEY_ISLAND_ABS_PATH, "cc") diff --git a/monkey/monkey_island/cc/server_utils/island_logger.py b/monkey/monkey_island/cc/server_utils/island_logger.py index a32f6505f..4405db5e1 100644 --- a/monkey/monkey_island/cc/server_utils/island_logger.py +++ b/monkey/monkey_island/cc/server_utils/island_logger.py @@ -1,38 +1,59 @@ -import json import logging.config import os +from copy import deepcopy from typing import Dict -from monkey_island.cc.server_utils.consts import DEFAULT_LOGGER_CONFIG_PATH - __author__ = "Maor.Rayzin" -def json_setup_logging( - default_path=DEFAULT_LOGGER_CONFIG_PATH, - default_level=logging.INFO, - env_key="LOG_CFG", +LOGGER_CONFIG_DICT = { + "version": 1, + "disable_existing_loggers": False, + "formatters": { + "simple": { + "format": "%(asctime)s - %(filename)s:%(lineno)s - " + + "%(funcName)10s() - %(levelname)s - %(message)s" + } + }, + "handlers": { + "console": { + "class": "logging.StreamHandler", + "level": "DEBUG", + "formatter": "simple", + "stream": "ext://sys.stdout", + }, + "info_file_handler": { + "class": "logging.handlers.RotatingFileHandler", + "level": "INFO", + "formatter": "simple", + "filename": None, # set in setup_logging() + "maxBytes": 10485760, + "backupCount": 20, + "encoding": "utf8", + }, + }, + "root": {"level": None, "handlers": ["console", "info_file_handler"]}, # set in setup_logging() +} + + +def setup_logging( + data_dir_path, + log_level, ): """ Setup the logging configuration - :param default_path: the default log configuration file path - :param default_level: Default level to log from - :param env_key: SYS ENV key to use for external configuration file path + :param data_dir_path: data directory file path + :param log_level: level to log from :return: """ - path = os.path.expanduser(default_path) - value = os.getenv(env_key, None) - if value: - path = value - - if os.path.exists(path): - with open(path, "rt") as f: - config = json.load(f) - _expanduser_log_file_paths(config) - logging.config.dictConfig(config) - else: - logging.basicConfig(level=default_level) + logger_configuration = deepcopy(LOGGER_CONFIG_DICT) + _expanduser_log_file_paths(logger_configuration) + logger_configuration["root"]["level"] = log_level + logger_configuration["handlers"]["info_file_handler"]["filename"] = os.path.join( + data_dir_path, "monkey_island.log" + ) + logging.config.dictConfig(logger_configuration) def _expanduser_log_file_paths(config: Dict):