forked from p15670423/monkey
Replace `json_setup_logging()` with `setup_logging()` to configure logger
This commit is contained in:
parent
ab89590389
commit
785f2ef77d
|
@ -10,7 +10,7 @@ from pathlib import Path # noqa: E402
|
||||||
|
|
||||||
import monkey_island.cc.environment.server_config_generator as server_config_generator # 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.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__:
|
if "__main__" == __name__:
|
||||||
island_args = parse_cli_args()
|
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
|
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:
|
except json.JSONDecodeError as ex:
|
||||||
print(f"Error loading logging config: {ex}")
|
print(f"Error loading logging config: {ex}")
|
||||||
|
|
|
@ -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"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -11,10 +11,6 @@ DEFAULT_DEVELOP_SERVER_CONFIG_PATH = os.path.join(
|
||||||
MONKEY_ISLAND_ABS_PATH, "cc", "server_config.json.develop"
|
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_LOG_LEVEL = "NOTSET"
|
||||||
|
|
||||||
DEFAULT_DATA_DIR = os.path.join(MONKEY_ISLAND_ABS_PATH, "cc")
|
DEFAULT_DATA_DIR = os.path.join(MONKEY_ISLAND_ABS_PATH, "cc")
|
||||||
|
|
|
@ -1,38 +1,59 @@
|
||||||
import json
|
|
||||||
import logging.config
|
import logging.config
|
||||||
import os
|
import os
|
||||||
|
from copy import deepcopy
|
||||||
from typing import Dict
|
from typing import Dict
|
||||||
|
|
||||||
from monkey_island.cc.server_utils.consts import DEFAULT_LOGGER_CONFIG_PATH
|
|
||||||
|
|
||||||
__author__ = "Maor.Rayzin"
|
__author__ = "Maor.Rayzin"
|
||||||
|
|
||||||
|
|
||||||
def json_setup_logging(
|
LOGGER_CONFIG_DICT = {
|
||||||
default_path=DEFAULT_LOGGER_CONFIG_PATH,
|
"version": 1,
|
||||||
default_level=logging.INFO,
|
"disable_existing_loggers": False,
|
||||||
env_key="LOG_CFG",
|
"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
|
Setup the logging configuration
|
||||||
:param default_path: the default log configuration file path
|
:param data_dir_path: data directory file path
|
||||||
:param default_level: Default level to log from
|
:param log_level: level to log from
|
||||||
:param env_key: SYS ENV key to use for external configuration file path
|
|
||||||
:return:
|
:return:
|
||||||
"""
|
"""
|
||||||
path = os.path.expanduser(default_path)
|
|
||||||
value = os.getenv(env_key, None)
|
|
||||||
|
|
||||||
if value:
|
logger_configuration = deepcopy(LOGGER_CONFIG_DICT)
|
||||||
path = value
|
_expanduser_log_file_paths(logger_configuration)
|
||||||
|
logger_configuration["root"]["level"] = log_level
|
||||||
if os.path.exists(path):
|
logger_configuration["handlers"]["info_file_handler"]["filename"] = os.path.join(
|
||||||
with open(path, "rt") as f:
|
data_dir_path, "monkey_island.log"
|
||||||
config = json.load(f)
|
)
|
||||||
_expanduser_log_file_paths(config)
|
logging.config.dictConfig(logger_configuration)
|
||||||
logging.config.dictConfig(config)
|
|
||||||
else:
|
|
||||||
logging.basicConfig(level=default_level)
|
|
||||||
|
|
||||||
|
|
||||||
def _expanduser_log_file_paths(config: Dict):
|
def _expanduser_log_file_paths(config: Dict):
|
||||||
|
|
Loading…
Reference in New Issue