Reworked setting log paths
* Added DEFAULT_LOG_DIR and DEFAULT_LOG_NAME vars * Allows easier reuse of default values * Added format_log_path() * Uses default path/name unless dir/name specified * Added get_root_logger_path() * Returns the first fileHandler path found (if any) * update_log_path() now supports multiple handler scenarios
This commit is contained in:
parent
2ea0b4818a
commit
60969f26eb
1 changed files with 63 additions and 45 deletions
|
|
@ -9,6 +9,22 @@ import shutil
|
||||||
import time
|
import time
|
||||||
|
|
||||||
from wk import cfg
|
from wk import cfg
|
||||||
|
from wk.io import non_clobber_path
|
||||||
|
|
||||||
|
# STATIC VARIABLES
|
||||||
|
if os.name == 'nt':
|
||||||
|
# Example: "C:\WK\1955-11-05\WizardKit"
|
||||||
|
DEFAULT_LOG_DIR = (
|
||||||
|
f'{os.environ.get("SYSTEMDRIVE", "C:")}/'
|
||||||
|
f'{cfg.main.KIT_NAME_SHORT}/'
|
||||||
|
f'{time.strftime("%Y-%m-%d")}/'
|
||||||
|
f'{cfg.main.KIT_NAME_FULL}'
|
||||||
|
)
|
||||||
|
DEFAULT_LOG_NAME = ''
|
||||||
|
else:
|
||||||
|
# Example: "/home/tech/Logs"
|
||||||
|
DEFAULT_LOG_DIR = f'{os.path.expanduser("~")}/Logs'
|
||||||
|
DEFAULT_LOG_NAME = cfg.main.KIT_NAME_FULL
|
||||||
|
|
||||||
|
|
||||||
# Functions
|
# Functions
|
||||||
|
|
@ -24,23 +40,43 @@ def enable_debug_mode():
|
||||||
root_logger.setLevel('DEBUG')
|
root_logger.setLevel('DEBUG')
|
||||||
|
|
||||||
|
|
||||||
|
def format_log_path(log_dir=None, log_name=None, tool=False, timestamp=True):
|
||||||
|
"""Format path based on args passed, returns pathlib.Path obj."""
|
||||||
|
log_path = pathlib.Path(
|
||||||
|
f'{log_dir if log_dir else DEFAULT_LOG_DIR}/'
|
||||||
|
f'{"Tools/" if tool else ""}'
|
||||||
|
f'{log_name if log_name else DEFAULT_LOG_NAME}'
|
||||||
|
f'{"_" if timestamp else ""}'
|
||||||
|
f'{time.strftime("%Y-%m-%d_%H%M%S%z") if timestamp else ""}'
|
||||||
|
'.log'
|
||||||
|
)
|
||||||
|
log_path = log_path.resolve()
|
||||||
|
|
||||||
|
# Avoid clobbering
|
||||||
|
log_path = non_clobber_path(log_path)
|
||||||
|
|
||||||
|
# Done
|
||||||
|
return log_path
|
||||||
|
|
||||||
|
|
||||||
|
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 start(config=None):
|
def start(config=None):
|
||||||
"""Configure and start logging using safe defaults."""
|
"""Configure and start logging using safe defaults."""
|
||||||
if os.name == 'nt':
|
log_path = format_log_path()
|
||||||
log_path = '{drive}/{short}/Logs/{date}/{full}/{datetime}.log'.format(
|
|
||||||
drive=os.environ.get('SYSTEMDRIVE', 'C:'),
|
|
||||||
short=cfg.main.KIT_NAME_SHORT,
|
|
||||||
date=time.strftime('%Y-%m-%d'),
|
|
||||||
full=cfg.main.KIT_NAME_FULL,
|
|
||||||
datetime=time.strftime('%Y-%m-%d_%H%M%S%z'),
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
log_path = '{home}/Logs/{full}_{datetime}.log'.format(
|
|
||||||
home=os.path.expanduser('~'),
|
|
||||||
full=cfg.main.KIT_NAME_FULL,
|
|
||||||
datetime=time.strftime('%Y-%m-%d_%H%M%S%z'),
|
|
||||||
)
|
|
||||||
log_path = pathlib.Path(log_path).resolve()
|
|
||||||
root_logger = logging.getLogger()
|
root_logger = logging.getLogger()
|
||||||
|
|
||||||
# Safety checks
|
# Safety checks
|
||||||
|
|
@ -59,38 +95,20 @@ def start(config=None):
|
||||||
atexit.register(logging.shutdown)
|
atexit.register(logging.shutdown)
|
||||||
|
|
||||||
|
|
||||||
def update_log_path(dest_dir=None, dest_name=None):
|
def update_log_path(dest_dir=None, dest_name=None, timestamp=True):
|
||||||
"""Moves current log file to new path and updates the root logger.
|
"""Moves current log file to new path and updates the root logger."""
|
||||||
|
|
||||||
NOTE: A timestamp and extension will be added to dest_name if provided.
|
|
||||||
"""
|
|
||||||
root_logger = logging.getLogger()
|
root_logger = logging.getLogger()
|
||||||
cur_handler = root_logger.handlers[0]
|
cur_handler = None
|
||||||
cur_path = pathlib.Path(cur_handler.baseFilename).resolve()
|
cur_path = get_root_logger_path()
|
||||||
|
new_path = format_log_path(dest_dir, dest_name, timestamp=timestamp)
|
||||||
|
|
||||||
# Safety checks
|
# Get current logging file handler
|
||||||
if not (dest_dir or dest_name):
|
for handler in root_logger.handlers:
|
||||||
raise RuntimeError('Neither a directory nor name specified')
|
if isinstance(handler, logging.FileHandler):
|
||||||
if len(root_logger.handlers) > 1:
|
cur_handler = handler
|
||||||
raise RuntimeError('Multiple handlers not supported')
|
break
|
||||||
if not isinstance(cur_handler, logging.FileHandler):
|
if not cur_handler:
|
||||||
raise RuntimeError('Only FileHandlers are supported')
|
raise RuntimeError('Logging FileHandler not found')
|
||||||
|
|
||||||
# Update dir if specified or use current path
|
|
||||||
if dest_dir:
|
|
||||||
new_path = pathlib.Path(dest_dir).resolve()
|
|
||||||
else:
|
|
||||||
new_path = cur_path
|
|
||||||
|
|
||||||
# Update name if specified
|
|
||||||
if dest_name:
|
|
||||||
new_path = new_path.with_name(
|
|
||||||
f'{dest_name}'
|
|
||||||
f'_{time.strftime("%Y-%m-%d_%H%M%S%z")}'
|
|
||||||
f'{"".join(cur_path.suffixes)}'
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
new_path = new_path.with_name(cur_path.name)
|
|
||||||
|
|
||||||
# Copy original log to new location
|
# Copy original log to new location
|
||||||
if new_path.exists():
|
if new_path.exists():
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue