From 55ccc2e6c5ae68cf2d2f31bdbecae0e041744b4f Mon Sep 17 00:00:00 2001 From: fabriziosalmi Date: Sat, 1 Feb 2025 11:56:08 +0100 Subject: [PATCH] logging setup breakfast --- lxc_autoscale/logging_setup.py | 51 ++++++++++++++++++++++++++++++++++ lxc_autoscale/lxc_autoscale.py | 18 ++++++++---- 2 files changed, 64 insertions(+), 5 deletions(-) create mode 100644 lxc_autoscale/logging_setup.py diff --git a/lxc_autoscale/logging_setup.py b/lxc_autoscale/logging_setup.py new file mode 100644 index 0000000..8b2a6d5 --- /dev/null +++ b/lxc_autoscale/logging_setup.py @@ -0,0 +1,51 @@ +"""Logging configuration for LXC autoscale.""" + +import logging +import logging.handlers +import os +from pathlib import Path +from typing import Optional + + +def setup_logging(log_file: Optional[str] = None, debug: bool = False) -> None: + """Configure logging with console and file handlers. + + Args: + log_file: Path to the log file. If None, only console logging is set up. + debug: Whether to enable debug logging. + """ + # Create formatter + formatter = logging.Formatter( + '%(asctime)s - %(name)s - %(levelname)s - %(message)s', + datefmt='%Y-%m-%d %H:%M:%S' + ) + + # Configure root logger + root_logger = logging.getLogger() + root_logger.setLevel(logging.DEBUG if debug else logging.INFO) + + # Add console handler + console_handler = logging.StreamHandler() + console_handler.setFormatter(formatter) + root_logger.addHandler(console_handler) + + # Add file handler if log_file is specified + if log_file: + # Ensure log directory exists + log_dir = os.path.dirname(log_file) + if log_dir: + Path(log_dir).mkdir(parents=True, exist_ok=True) + + # Create rotating file handler (10MB per file, max 5 files) + file_handler = logging.handlers.RotatingFileHandler( + log_file, + maxBytes=10*1024*1024, # 10MB + backupCount=5, + encoding='utf-8' + ) + file_handler.setFormatter(formatter) + root_logger.addHandler(file_handler) + + # Suppress excessive logging from third-party libraries + logging.getLogger('paramiko').setLevel(logging.WARNING) + logging.getLogger('urllib3').setLevel(logging.WARNING) diff --git a/lxc_autoscale/lxc_autoscale.py b/lxc_autoscale/lxc_autoscale.py index de0da5a..c838b97 100644 --- a/lxc_autoscale/lxc_autoscale.py +++ b/lxc_autoscale/lxc_autoscale.py @@ -44,6 +44,13 @@ def parse_arguments() -> argparse.Namespace: help="Rollback to previous container configurations" # Option to revert containers to their backed-up settings ) + # Add debug mode argument + parser.add_argument( + "--debug", + action="store_true", + help="Enable debug logging" + ) + args = parser.parse_args() logging.debug(f"Parsed arguments: {args}") return args @@ -51,13 +58,14 @@ def parse_arguments() -> argparse.Namespace: # Entry point of the script if __name__ == "__main__": - # Setup logging with the configured log file - setup_logging(LOG_FILE) - - # Parse command-line arguments + # Parse arguments first to get debug flag args: argparse.Namespace = parse_arguments() + + # Setup logging with the configured log file and debug mode + setup_logging(LOG_FILE, args.debug) - logging.info("Starting LXC autoscaling daemon with arguments: %s", args) + logging.info("Starting LXC autoscaling daemon") + logging.debug("Arguments: %s", args) # Acquire a lock to ensure that only one instance of the script runs at a time with acquire_lock() as lock_file: