-
Notifications
You must be signed in to change notification settings - Fork 8
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
Showing
2 changed files
with
257 additions
and
29 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
132 changes: 119 additions & 13 deletions
132
local-app/python-tools/gfl-resource-actions/logging_utils.py
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 |
|---|---|---|
| @@ -1,18 +1,124 @@ | ||
| """ | ||
| Logging utilities for AWS Resource Management. | ||
| Logging utilities for resource management. | ||
| """ | ||
| import logging | ||
| import os | ||
| import sys | ||
| import config | ||
| import csv | ||
| import datetime | ||
| from pathlib import Path | ||
|
|
||
| def setup_logging(): | ||
| """Configure logging for the application.""" | ||
| logging.basicConfig( | ||
| level=logging.INFO, | ||
| format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', | ||
| handlers=[ | ||
| logging.FileHandler(config.LOG_FILE), | ||
| logging.StreamHandler(sys.stdout) | ||
| ] | ||
| ) | ||
| return logging.getLogger("AWS-Resource-Management") | ||
| # Configure log formats | ||
| DEFAULT_LOG_FORMAT = '%(asctime)s - %(name)s - %(levelname)s - %(message)s' | ||
| DEFAULT_LOG_LEVEL = logging.INFO | ||
|
|
||
| # Configure CSV logging | ||
| CSV_LOG_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'logs') | ||
| ACTION_CSV_FILE = 'resource_actions.csv' | ||
| CSV_FIELDS = ['timestamp', 'account_id', 'account_name', 'resource_type', 'resource_id', 'resource_name', 'action', 'region', 'status', 'details'] | ||
|
|
||
| def setup_logging(log_level=None, log_format=None): | ||
| """ | ||
| Set up logging configuration. | ||
| Args: | ||
| log_level (int, optional): Logging level (default: INFO) | ||
| log_format (str, optional): Log message format | ||
| Returns: | ||
| logging.Logger: Configured logger | ||
| """ | ||
| if log_level is None: | ||
| log_level = DEFAULT_LOG_LEVEL | ||
|
|
||
| if log_format is None: | ||
| log_format = DEFAULT_LOG_FORMAT | ||
|
|
||
| # Create logger | ||
| logger = logging.getLogger('resource-management') | ||
| logger.setLevel(log_level) | ||
|
|
||
| # Create console handler | ||
| console_handler = logging.StreamHandler(sys.stdout) | ||
| console_handler.setLevel(log_level) | ||
|
|
||
| # Create formatter and add it to the handler | ||
| formatter = logging.Formatter(log_format) | ||
| console_handler.setFormatter(formatter) | ||
|
|
||
| # Add handler to logger | ||
| logger.addHandler(console_handler) | ||
|
|
||
| # Ensure CSV log directory exists | ||
| os.makedirs(CSV_LOG_DIR, exist_ok=True) | ||
|
|
||
| return logger | ||
|
|
||
| def initialize_csv_log(csv_file=None): | ||
| """ | ||
| Initialize CSV log file with headers if it doesn't exist. | ||
| Args: | ||
| csv_file (str, optional): CSV file name (default: resource_actions.csv) | ||
| Returns: | ||
| str: Path to the CSV log file | ||
| """ | ||
| if csv_file is None: | ||
| csv_file = ACTION_CSV_FILE | ||
|
|
||
| csv_path = os.path.join(CSV_LOG_DIR, csv_file) | ||
|
|
||
| # Create directory if it doesn't exist | ||
| Path(CSV_LOG_DIR).mkdir(parents=True, exist_ok=True) | ||
|
|
||
| # Check if file exists, if not create with headers | ||
| file_exists = os.path.isfile(csv_path) | ||
|
|
||
| if not file_exists: | ||
| with open(csv_path, 'w', newline='') as f: | ||
| writer = csv.writer(f) | ||
| writer.writerow(CSV_FIELDS) | ||
|
|
||
| return csv_path | ||
|
|
||
| def log_action_to_csv(account_id, account_name, resource_type, resource_id, | ||
| resource_name, action, region, status, details='', csv_file=None): | ||
| """ | ||
| Log an action to the CSV file. | ||
| Args: | ||
| account_id (str): AWS account ID | ||
| account_name (str): AWS account name | ||
| resource_type (str): Type of resource (e.g., 'ec2', 'rds') | ||
| resource_id (str): Resource identifier | ||
| resource_name (str): Resource name | ||
| action (str): Action performed (e.g., 'discover', 'stop', 'start') | ||
| region (str): AWS region | ||
| status (str): Status of the action (e.g., 'success', 'failed') | ||
| details (str, optional): Additional details | ||
| csv_file (str, optional): CSV file name (default: resource_actions.csv) | ||
| """ | ||
| if csv_file is None: | ||
| csv_file = ACTION_CSV_FILE | ||
|
|
||
| csv_path = initialize_csv_log(csv_file) | ||
| timestamp = datetime.datetime.now().isoformat() | ||
|
|
||
| with open(csv_path, 'a', newline='') as f: | ||
| writer = csv.writer(f) | ||
| writer.writerow([ | ||
| timestamp, | ||
| account_id, | ||
| account_name, | ||
| resource_type, | ||
| resource_id, | ||
| resource_name, | ||
| action, | ||
| region, | ||
| status, | ||
| details | ||
| ]) | ||
|
|
||
| # Create a logger instance for direct import | ||
| logger = setup_logging() |