1. import logging
  2. import json
  3. def export_results(results, filename="diagnostics.json"):
  4. """
  5. Exports a list of results to a JSON file with error logging.
  6. Args:
  7. results (list): A list of dictionaries containing the results.
  8. filename (str): The name of the output JSON file.
  9. """
  10. logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
  11. try:
  12. with open(filename, 'w') as f:
  13. json.dump(results, f, indent=4) # Write results to JSON file with indentation
  14. logging.info(f"Results exported to {filename} successfully.")
  15. except Exception as e:
  16. logging.error(f"Error exporting results to {filename}: {e}")
  17. if __name__ == '__main__':
  18. # Example usage
  19. sample_results = [
  20. {"test_name": "Test 1", "result": "Pass", "value": 10},
  21. {"test_name": "Test 2", "result": "Fail", "value": 20},
  22. {"test_name": "Test 3", "result": "Pass", "value": 30}
  23. ]
  24. export_results(sample_results, "my_results.json")

Add your comment