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.

Easy Python 3.9+ Aug 9, 2026 Automation & scripting 20 views 0 copies

Python code

29 lines
Python 3.9+
import 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

stdout
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

  1. Use a generator expression with `Counter(match.group(1) for match in ...)` for more concise code.
  2. 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

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from Automation & scripting

Related tutorials and quizzes for this topic.