Island: Simplify logic in get_log_file_path()

This commit is contained in:
Shreya Malviya 2022-08-01 16:51:35 +05:30
parent acade35fbe
commit 1d11dd227e
2 changed files with 6 additions and 24 deletions

View File

@ -2,7 +2,6 @@ import logging
import logging.handlers
import sys
from pathlib import Path
from typing import Optional
ISLAND_LOG_FILENAME = "monkey_island.log"
LOG_FORMAT = "%(asctime)s - %(levelname)s - %(filename)s:%(lineno)s - %(funcName)s() - %(message)s"
@ -24,12 +23,16 @@ def setup_logging(data_dir: Path, log_level: str):
formatter = _get_log_formatter()
log_file_path = data_dir / ISLAND_LOG_FILENAME
log_file_path = get_log_file_path(data_dir)
_add_file_handler(logger, formatter, log_file_path)
_add_console_handler(logger, formatter)
def get_log_file_path(data_dir: Path) -> Path:
return data_dir / ISLAND_LOG_FILENAME
def setup_default_failsafe_logging():
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
@ -64,24 +67,3 @@ def reset_logger():
for handler in logger.handlers:
logger.removeHandler(handler)
def get_log_file_path() -> Optional[Path]:
"""
Finds the log file by finding the logger handlers and checking if one of them is a fileHandler
of any kind by checking if the handler has the property handler.baseFilename.
:return: Log file path
"""
logger = logging.getLogger(__name__)
logger_handlers = logger.parent.handlers
for handler in logger_handlers:
if hasattr(handler, "baseFilename"):
logger.info("Log file found: {0}".format(handler.baseFilename))
log_file_path = handler.baseFilename
return Path(log_file_path)
logger.warning("No log file could be found, check logger config.")
return None

View File

@ -88,7 +88,7 @@ def _register_conventions(container: DIContainer, data_dir: Path):
"default_ransomware_agent_configuration",
DEFAULT_RANSOMWARE_AGENT_CONFIGURATION,
)
container.register_convention(Path, "island_log_file_path", get_log_file_path())
container.register_convention(Path, "island_log_file_path", get_log_file_path(data_dir))
def _register_repositories(container: DIContainer, data_dir: Path):