Remove duplicate function wk.log.get_log_filepath

This commit is contained in:
2Shirt 2023-05-27 21:12:27 -07:00
parent 0126452bf1
commit 69832eda5d
Signed by: 2Shirt
GPG key ID: 152FAC923B0E132C
2 changed files with 18 additions and 26 deletions

View file

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

View file

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