moonraker: gracefully handle invalid log paths
Don't exit the server if the log path is invalid. Instead start logging to stdout and add a warning for the user to resolve. Signed-off-by: Eric Callahan <arksine.code@gmail.com>
This commit is contained in:
parent
305e002f8c
commit
ae8cab356b
|
@ -122,6 +122,10 @@ class Server:
|
||||||
self.register_upload_handler = app.register_upload_handler
|
self.register_upload_handler = app.register_upload_handler
|
||||||
self.register_api_transport = app.register_api_transport
|
self.register_api_transport = app.register_api_transport
|
||||||
|
|
||||||
|
log_warn: Optional[str] = args.get('log_warning')
|
||||||
|
if log_warn is not None:
|
||||||
|
self.add_warning(log_warn)
|
||||||
|
|
||||||
self.register_endpoint(
|
self.register_endpoint(
|
||||||
"/server/info", ['GET'], self._handle_info_request)
|
"/server/info", ['GET'], self._handle_info_request)
|
||||||
self.register_endpoint(
|
self.register_endpoint(
|
||||||
|
@ -854,7 +858,8 @@ def main() -> None:
|
||||||
app_args['log_file'] = os.path.normpath(
|
app_args['log_file'] = os.path.normpath(
|
||||||
os.path.expanduser(cmd_line_args.logfile))
|
os.path.expanduser(cmd_line_args.logfile))
|
||||||
app_args['software_version'] = version
|
app_args['software_version'] = version
|
||||||
ql, file_logger = utils.setup_logging(app_args)
|
ql, file_logger, warning = utils.setup_logging(app_args)
|
||||||
|
app_args['log_warning'] = warning
|
||||||
|
|
||||||
if sys.version_info < (3, 7):
|
if sys.version_info < (3, 7):
|
||||||
msg = f"Moonraker requires Python 3.7 or above. " \
|
msg = f"Moonraker requires Python 3.7 or above. " \
|
||||||
|
|
|
@ -138,7 +138,8 @@ def get_software_version() -> str:
|
||||||
|
|
||||||
def setup_logging(app_args: Dict[str, Any]
|
def setup_logging(app_args: Dict[str, Any]
|
||||||
) -> Tuple[logging.handlers.QueueListener,
|
) -> Tuple[logging.handlers.QueueListener,
|
||||||
Optional[MoonrakerLoggingHandler]]:
|
Optional[MoonrakerLoggingHandler],
|
||||||
|
Optional[str]]:
|
||||||
root_logger = logging.getLogger()
|
root_logger = logging.getLogger()
|
||||||
queue: Queue = Queue()
|
queue: Queue = Queue()
|
||||||
queue_handler = LocalQueueHandler(queue)
|
queue_handler = LocalQueueHandler(queue)
|
||||||
|
@ -150,20 +151,30 @@ def setup_logging(app_args: Dict[str, Any]
|
||||||
stdout_hdlr.setFormatter(stdout_fmt)
|
stdout_hdlr.setFormatter(stdout_fmt)
|
||||||
for name, val in app_args.items():
|
for name, val in app_args.items():
|
||||||
logging.info(f"{name}: {val}")
|
logging.info(f"{name}: {val}")
|
||||||
file_hdlr = None
|
warning: Optional[str] = None
|
||||||
if app_args.get('log_file', ""):
|
file_hdlr: Optional[MoonrakerLoggingHandler] = None
|
||||||
file_hdlr = MoonrakerLoggingHandler(
|
listener: Optional[logging.handlers.QueueListener] = None
|
||||||
app_args, when='midnight', backupCount=2)
|
log_file: str = app_args.get('log_file', "")
|
||||||
formatter = logging.Formatter(
|
if log_file:
|
||||||
'%(asctime)s [%(filename)s:%(funcName)s()] - %(message)s')
|
try:
|
||||||
file_hdlr.setFormatter(formatter)
|
file_hdlr = MoonrakerLoggingHandler(
|
||||||
listener = logging.handlers.QueueListener(
|
app_args, when='midnight', backupCount=2)
|
||||||
queue, file_hdlr, stdout_hdlr)
|
formatter = logging.Formatter(
|
||||||
else:
|
'%(asctime)s [%(filename)s:%(funcName)s()] - %(message)s')
|
||||||
|
file_hdlr.setFormatter(formatter)
|
||||||
|
listener = logging.handlers.QueueListener(
|
||||||
|
queue, file_hdlr, stdout_hdlr)
|
||||||
|
except Exception:
|
||||||
|
log_file = os.path.normpath(log_file)
|
||||||
|
dir_name = os.path.dirname(log_file)
|
||||||
|
warning = f"Unable to create log file at '{log_file}'. " \
|
||||||
|
f"Make sure that the folder '{dir_name}' exists " \
|
||||||
|
f"and Moonraker has Read/Write access to the folder. "
|
||||||
|
if listener is None:
|
||||||
listener = logging.handlers.QueueListener(
|
listener = logging.handlers.QueueListener(
|
||||||
queue, stdout_hdlr)
|
queue, stdout_hdlr)
|
||||||
listener.start()
|
listener.start()
|
||||||
return listener, file_hdlr
|
return listener, file_hdlr, warning
|
||||||
|
|
||||||
def hash_directory(dir_path: str,
|
def hash_directory(dir_path: str,
|
||||||
ignore_exts: List[str],
|
ignore_exts: List[str],
|
||||||
|
|
Loading…
Reference in New Issue