Aggregate Log Errors Count by Hour in Python
Counts ERROR log lines per hour using regex and Counter, returning a sorted dictionary of hourly totals.
Python code
29 linesimport re
from collections import Counter
from datetime import datetime
def aggregate_errors_by_hour(log_lines):
pattern = re.compile(r'^(\d{4}-\d{2}-\d{2} \d{2}):\d{2}:\d{2}.*ERROR')
hourly_counts = Counter()
for line in log_lines:
match = pattern.match(line)
if match:
hour = match.group(1)
hourly_counts[hour] += 1
return dict(sorted(hourly_counts.items()))
if __name__ == "__main__":
sample_logs = [
"2024-01-15 08:23:45 ERROR Database connection failed",
"2024-01-15 08:45:10 INFO Retrying connection",
"2024-01-15 09:12:30 ERROR Timeout occurred",
"2024-01-15 09:45:55 ERROR Invalid response",
"2024-01-15 10:01:20 ERROR Disk full",
"2024-01-15 09:30:00 WARNING High memory usage"
]
result = aggregate_errors_by_hour(sample_logs)
for hour, count in result.items():
print(f"{hour}: {count}")
Output
2024-01-15 08: 1
2024-01-15 09: 2
2024-01-15 10: 1
How it works
The regex pattern matches lines starting with an ISO timestamp and containing 'ERROR' after the time, using re.match to anchor at the start. The Counter from collections tallies each hour key. Sorting the dictionary items ensures chronological output. This works efficiently even for large log files because each line is processed once with minimal overhead.
Common mistakes
- Forgetting to anchor the regex with `^`, causing matches in the middle of lines.
- Using `$` to match end of line when the pattern doesn't span the whole line.
- Not handling timezone-aware timestamps if logs include offsets.
Variations
- Use a generator expression with `Counter(match.group(1) for match in ...)` for more concise code.
- Support custom log formats by adjusting the regex pattern and group index.
Real-world use cases
- Monitoring production systems to detect error spikes in a given hour for alerting.
- Analyzing application logs in batch jobs to report daily error trends by hour.
- Building a simple dashboard that visualizes error frequency per hour from log files.
Sponsored
More from Automation & scripting
- Automate Tweeting New Blog Posts in Python easy
- Automatically Clean Temporary Files from Applications Using Python medium
- Automatically Download the Latest Software Release from GitHub with Python medium
- Automatically Generate Charts from CSV Files with One Command medium
- Automatically Generate Hardware Inventory Reports in Python easy
- Automatically Log CPU, RAM, and Disk Usage Every Minute in Python easy
Keep learning
Related tutorials and quizzes for this topic.