RWayne93/azure-automation

Add Logging

Opened this issue · 1 comments

Issue

Add Logging

Discussion

Logging would make debugging easier.

Recommendation(s)

  1. Create a logging package and handler.
  2. Log all successful and attempted changes made to Azure-based resources.
  3. Use JSON.
  4. Use a configurable path for the logging destination.
import logging
import os

# Configure logging
log_file_path = os.getenv('LOG_PATH', './vm_creation_logs.json')
logging.basicConfig(filename=log_file_path, level=logging.INFO,
                    format='{"time": "%(asctime)s", "level": "%(levelname)s", "message": "%(message)s"}')

class AzureVM:

    def create_virtual_machine(self, resource_group_name, vm_name, admin_user, admin_password, ports=[], image_data=None, vm_size=None):
        try:
            logging.info(f"VM {vm_name} created with ID: {vm_info.id}")
        except Exception as e:
            logging.error(f"Failed to create VM {vm_name}. Error: {str(e)}")

For the VM i was thinking something like this maybe?