WizardKit/scripts/wk/log.py
2Shirt 60969f26eb
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
2019-09-22 23:27:41 -07:00

129 lines
3.5 KiB
Python

"""WizardKit: Log Functions"""
# vim: sts=2 sw=2 ts=2
import atexit
import logging
import os
import pathlib
import shutil
import time
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
def enable_debug_mode():
"""Configures logging for better debugging."""
root_logger = logging.getLogger()
for handler in root_logger.handlers:
formatter = logging.Formatter(
datefmt=cfg.log.DEBUG['datefmt'],
fmt=cfg.log.DEBUG['format'],
)
handler.setFormatter(formatter)
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):
"""Configure and start logging using safe defaults."""
log_path = format_log_path()
root_logger = logging.getLogger()
# Safety checks
if not config:
config = cfg.log.DEFAULT
if root_logger.hasHandlers():
raise UserWarning('Logging already started, results may be unpredictable.')
# Create log_dir
os.makedirs(log_path.parent, exist_ok=True)
# Config logger
logging.basicConfig(filename=log_path, **config)
# Register shutdown to run atexit
atexit.register(logging.shutdown)
def update_log_path(dest_dir=None, dest_name=None, timestamp=True):
"""Moves current log file to new path and updates the root logger."""
root_logger = logging.getLogger()
cur_handler = None
cur_path = get_root_logger_path()
new_path = format_log_path(dest_dir, dest_name, timestamp=timestamp)
# Get current logging file handler
for handler in root_logger.handlers:
if isinstance(handler, logging.FileHandler):
cur_handler = handler
break
if not cur_handler:
raise RuntimeError('Logging FileHandler not found')
# Copy original log to new location
if new_path.exists():
raise FileExistsError(f'Refusing to clobber: {new_path}')
os.makedirs(new_path.parent, exist_ok=True)
shutil.move(cur_path, new_path)
# Create new cur_handler (preserving formatter settings)
new_handler = logging.FileHandler(new_path, mode='a')
new_handler.setFormatter(cur_handler.formatter)
# Replace current handler
root_logger.removeHandler(cur_handler)
root_logger.addHandler(new_handler)
if __name__ == '__main__':
print("This file is not meant to be called directly.")