Skip to content

Commit

Permalink
logging setup breakfast
Browse files Browse the repository at this point in the history
  • Loading branch information
fabriziosalmi committed Feb 1, 2025
1 parent a697d7c commit 55ccc2e
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 5 deletions.
51 changes: 51 additions & 0 deletions lxc_autoscale/logging_setup.py
Original file line number Diff line number Diff line change
@@ -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)
18 changes: 13 additions & 5 deletions lxc_autoscale/lxc_autoscale.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,28 @@ 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


# 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:
Expand Down

0 comments on commit 55ccc2e

Please sign in to comment.