27 lines
2.2 KiB
Plaintext
Raw Normal View History

2026-01-29 06:54:10 +01:00
TASK: Create a dummy log file with 20 lines of mixed INFO and ERROR messages. Parse it using Python to count errors and save a JSON summary to 'log_analysis.json'.
----------------------------------------
Loading...
2026-01-29 07:42:06 +01:00
┌─── Python Source Code ─────────────────────────────────────
│  1 | import json
2026-02-10 04:29:48 +01:00
│  2 | # Create dummy log file
│  3 | log_lines = [f"INFO: Message {i}" if i % 2 == 0 else f"ERROR: Message {i}" for i in range(20)]
│  4 | with open('dummy_log.txt', 'w') as f:
│  5 | f.write('\n'.join(log_lines))
│  6 | # Parse log file to count errors
│  7 | error_count = 0
│  8 | with open('dummy_log.txt', 'r') as f:
│  9 | for line in f:
│  10 | if line.startswith('ERROR'):
│  11 | error_count += 1
│  12 | # Create JSON summary
│  13 | summary = {
│  14 | "total_lines": 20,
│  15 | "error_count": error_count
│  16 | }
│  17 | # Save summary to JSON file
│  18 | with open('log_analysis.json', 'w') as f:
│  19 | json.dump(summary, f)
2026-01-29 07:42:06 +01:00
└────────────────────────────────────────────────────────────
2026-02-10 04:29:48 +01:00
A dummy log file with 20 lines of mixed INFO and ERROR messages has been created. The script parsed the log to count the errors and saved the summary as JSON in 'log_analysis.json'.