-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a697d7c
commit 55ccc2e
Showing
2 changed files
with
64 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters