Remove duplicate function wk.log.get_log_filepath
This commit is contained in:
parent
0126452bf1
commit
69832eda5d
2 changed files with 18 additions and 26 deletions
|
|
@ -15,7 +15,7 @@ import time
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
from wk.cfg.net import CRASH_SERVER
|
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
|
# Classes
|
||||||
class Debug():
|
class Debug():
|
||||||
|
|
@ -42,8 +42,12 @@ def generate_debug_report():
|
||||||
report = []
|
report = []
|
||||||
|
|
||||||
# Logging data
|
# Logging data
|
||||||
log_path = get_log_filepath()
|
try:
|
||||||
if log_path:
|
log_path = get_root_logger_path()
|
||||||
|
except RuntimeError:
|
||||||
|
# Assuming logging wasn't started
|
||||||
|
pass
|
||||||
|
else:
|
||||||
report.append('------ Start Log -------')
|
report.append('------ Start Log -------')
|
||||||
report.append('')
|
report.append('')
|
||||||
with open(log_path, 'r', encoding='utf-8') as log_file:
|
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)
|
# Set filename (based on the logging config if possible)
|
||||||
filename = 'Unknown'
|
filename = 'Unknown'
|
||||||
log_path = get_log_filepath()
|
try:
|
||||||
if log_path:
|
log_path = get_root_logger_path()
|
||||||
|
except RuntimeError:
|
||||||
|
# Assuming logging wasn't started
|
||||||
|
pass
|
||||||
|
else:
|
||||||
# Strip everything but the prefix
|
# Strip everything but the prefix
|
||||||
filename = re.sub(r'^(.*)_(\d{4}-\d{2}-\d{2}.*)', r'\1', log_path.name)
|
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'
|
filename = f'{filename}_{reason}_{time.strftime("%Y-%m-%d_%H%M%S%z")}.log'
|
||||||
|
|
|
||||||
|
|
@ -61,37 +61,21 @@ def format_log_path(
|
||||||
return 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.
|
"""Get the log filepath from the root logger, returns pathlib.Path obj.
|
||||||
|
|
||||||
NOTE: This will use the first handler baseFilename it finds (if any).
|
NOTE: This will use the first handler baseFilename it finds (if any).
|
||||||
"""
|
"""
|
||||||
log_filepath = None
|
|
||||||
root_logger = logging.getLogger()
|
root_logger = logging.getLogger()
|
||||||
|
|
||||||
# Check handlers
|
# Check handlers
|
||||||
for handler in root_logger.handlers:
|
for handler in root_logger.handlers:
|
||||||
if hasattr(handler, 'baseFilename'):
|
if hasattr(handler, 'baseFilename'):
|
||||||
log_filepath = pathlib.Path(handler.baseFilename).resolve()
|
log_file = handler.baseFilename # type: ignore[reportGeneralTypeIssues]
|
||||||
break
|
return pathlib.Path(log_file).resolve()
|
||||||
|
|
||||||
# Done
|
# No log file found
|
||||||
return log_filepath
|
raise RuntimeError('Log path not found.')
|
||||||
|
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
|
|
||||||
def remove_empty_log(log_path=None):
|
def remove_empty_log(log_path=None):
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue