diff --git a/scripts/wk/debug.py b/scripts/wk/debug.py index 69f400d8..0a339229 100644 --- a/scripts/wk/debug.py +++ b/scripts/wk/debug.py @@ -15,7 +15,7 @@ import time import requests from wk.cfg.net import CRASH_SERVER -from wk.log import get_log_filepath, get_root_logger_path +from wk.log import get_root_logger_path # Classes class Debug(): @@ -42,8 +42,12 @@ def generate_debug_report(): report = [] # Logging data - log_path = get_log_filepath() - if log_path: + try: + log_path = get_root_logger_path() + except RuntimeError: + # Assuming logging wasn't started + pass + else: report.append('------ Start Log -------') report.append('') with open(log_path, 'r', encoding='utf-8') as log_file: @@ -140,8 +144,12 @@ def upload_debug_report(report, compress=True, reason='DEBUG'): # Set filename (based on the logging config if possible) filename = 'Unknown' - log_path = get_log_filepath() - if log_path: + try: + log_path = get_root_logger_path() + except RuntimeError: + # Assuming logging wasn't started + pass + else: # Strip everything but the prefix filename = re.sub(r'^(.*)_(\d{4}-\d{2}-\d{2}.*)', r'\1', log_path.name) filename = f'{filename}_{reason}_{time.strftime("%Y-%m-%d_%H%M%S%z")}.log' diff --git a/scripts/wk/log.py b/scripts/wk/log.py index 0b3e7e06..96ee251a 100644 --- a/scripts/wk/log.py +++ b/scripts/wk/log.py @@ -61,37 +61,21 @@ def format_log_path( return log_path -def get_log_filepath(): +def get_root_logger_path(): """Get the log filepath from the root logger, returns pathlib.Path obj. NOTE: This will use the first handler baseFilename it finds (if any). """ - log_filepath = None root_logger = logging.getLogger() # Check handlers for handler in root_logger.handlers: if hasattr(handler, 'baseFilename'): - log_filepath = pathlib.Path(handler.baseFilename).resolve() - break + log_file = handler.baseFilename # type: ignore[reportGeneralTypeIssues] + return pathlib.Path(log_file).resolve() - # Done - return log_filepath - - -def get_root_logger_path(): - """Get path to log file from root logger, returns pathlib.Path obj.""" - log_path = None - root_logger = logging.getLogger() - - # Check all handlers and use the first fileHandler found - for handler in root_logger.handlers: - if isinstance(handler, logging.FileHandler): - log_path = pathlib.Path(handler.baseFilename).resolve() - break - - # Done - return log_path + # No log file found + raise RuntimeError('Log path not found.') def remove_empty_log(log_path=None):