forked from p15670423/monkey
Island: Add get_log_file() to cc/server_utils/island_logger.py
This commit is contained in:
parent
e7e424b358
commit
988f393a20
|
@ -2,6 +2,7 @@ import logging
|
||||||
import logging.handlers
|
import logging.handlers
|
||||||
import sys
|
import sys
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
from typing import Mapping
|
||||||
|
|
||||||
ISLAND_LOG_FILENAME = "monkey_island.log"
|
ISLAND_LOG_FILENAME = "monkey_island.log"
|
||||||
LOG_FORMAT = "%(asctime)s - %(levelname)s - %(filename)s:%(lineno)s - %(funcName)s() - %(message)s"
|
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:
|
for handler in logger.handlers:
|
||||||
logger.removeHandler(handler)
|
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
|
||||||
|
|
Loading…
Reference in New Issue