From 988f393a20bd88af0de46093f444555981fdccf9 Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Mon, 1 Aug 2022 13:17:44 +0530 Subject: [PATCH] Island: Add get_log_file() to cc/server_utils/island_logger.py --- .../cc/server_utils/island_logger.py | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/monkey/monkey_island/cc/server_utils/island_logger.py b/monkey/monkey_island/cc/server_utils/island_logger.py index 513d5031c..c0f55a3aa 100644 --- a/monkey/monkey_island/cc/server_utils/island_logger.py +++ b/monkey/monkey_island/cc/server_utils/island_logger.py @@ -2,6 +2,7 @@ import logging import logging.handlers import sys from pathlib import Path +from typing import Mapping ISLAND_LOG_FILENAME = "monkey_island.log" LOG_FORMAT = "%(asctime)s - %(levelname)s - %(filename)s:%(lineno)s - %(funcName)s() - %(message)s" @@ -63,3 +64,26 @@ def reset_logger(): for handler in logger.handlers: logger.removeHandler(handler) + + +def get_log_file() -> Mapping: + """ + This is a helper function for the Monkey Island log download function. + It finds the logger handlers and checks if one of them is a fileHandler of any kind by + checking if the handler has the property handler.baseFilename. + + :return: A dict with log file contents + """ + 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 + with open(log_file_path, "rt") as f: + log_file = f.read() + return {"log_file": log_file} + + logger.warning("No log file could be found, check logger config.") + return None