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