diff --git a/moonraker/moonraker.py b/moonraker/moonraker.py index 173d70c..f05d7fe 100755 --- a/moonraker/moonraker.py +++ b/moonraker/moonraker.py @@ -122,6 +122,10 @@ class Server: self.register_upload_handler = app.register_upload_handler 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( "/server/info", ['GET'], self._handle_info_request) self.register_endpoint( @@ -854,7 +858,8 @@ def main() -> None: app_args['log_file'] = os.path.normpath( os.path.expanduser(cmd_line_args.logfile)) 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): msg = f"Moonraker requires Python 3.7 or above. " \ diff --git a/moonraker/utils.py b/moonraker/utils.py index 3b0f5fa..3e72ea5 100644 --- a/moonraker/utils.py +++ b/moonraker/utils.py @@ -138,7 +138,8 @@ def get_software_version() -> str: def setup_logging(app_args: Dict[str, Any] ) -> Tuple[logging.handlers.QueueListener, - Optional[MoonrakerLoggingHandler]]: + Optional[MoonrakerLoggingHandler], + Optional[str]]: root_logger = logging.getLogger() queue: Queue = Queue() queue_handler = LocalQueueHandler(queue) @@ -150,20 +151,30 @@ def setup_logging(app_args: Dict[str, Any] stdout_hdlr.setFormatter(stdout_fmt) for name, val in app_args.items(): logging.info(f"{name}: {val}") - file_hdlr = None - if app_args.get('log_file', ""): - file_hdlr = MoonrakerLoggingHandler( - app_args, when='midnight', backupCount=2) - formatter = logging.Formatter( - '%(asctime)s [%(filename)s:%(funcName)s()] - %(message)s') - file_hdlr.setFormatter(formatter) - listener = logging.handlers.QueueListener( - queue, file_hdlr, stdout_hdlr) - else: + warning: Optional[str] = None + file_hdlr: Optional[MoonrakerLoggingHandler] = None + listener: Optional[logging.handlers.QueueListener] = None + log_file: str = app_args.get('log_file', "") + if log_file: + try: + file_hdlr = MoonrakerLoggingHandler( + app_args, when='midnight', backupCount=2) + formatter = logging.Formatter( + '%(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( queue, stdout_hdlr) listener.start() - return listener, file_hdlr + return listener, file_hdlr, warning def hash_directory(dir_path: str, ignore_exts: List[str],