1. import logging
  2. import json
  3. def configure_sandbox_logging(config_file):
  4. """
  5. Replaces configuration values for sandbox usage with verbose logging.
  6. Args:
  7. config_file (str): Path to the configuration file (JSON).
  8. """
  9. # Set up logging
  10. logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
  11. try:
  12. with open(config_file, 'r') as f:
  13. config = json.load(f)
  14. except FileNotFoundError:
  15. logging.error(f"Configuration file not found: {config_file}")
  16. return
  17. except json.JSONDecodeError:
  18. logging.error(f"Invalid JSON format in configuration file: {config_file}")
  19. return
  20. # Iterate through the configuration and replace values for sandbox
  21. for key, value in config.items():
  22. if "sandbox" in key.lower() or "sandbox_" in key.lower():
  23. logging.debug(f"Configuring {key} for sandbox mode.")
  24. if isinstance(value, str):
  25. config[key] = f"sandbox_value_{value}" # Replace with a sandbox-specific value
  26. elif isinstance(value, int):
  27. config[key] = value * 10 # Multiply by 10 for sandbox
  28. elif isinstance(value, bool):
  29. config[key] = True #Set to True for sandbox
  30. else:
  31. config[key] = "sandbox_default" #Default sandbox value
  32. # Output the modified configuration (optional)
  33. print(json.dumps(config, indent=4))
  34. # Save the modified configuration back to the file (optional)
  35. try:
  36. with open(config_file, 'w') as f:
  37. json.dump(config, f, indent=4)
  38. except Exception as e:
  39. logging.error(f"Error saving configuration file: {e}")
  40. if __name__ == '__main__':
  41. # Example usage: Replace 'config.json' with your actual config file
  42. configure_sandbox_logging('config.json')

Add your comment