Island: Add get_log_file() to cc/server_utils/island_logger.py

This commit is contained in:
Shreya Malviya 2022-08-01 13:17:44 +05:30
parent e7e424b358
commit 988f393a20
1 changed files with 24 additions and 0 deletions

View File

@ -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