How to Generate a Mock Rollbar Error Report in Python
Create a realistic fake Rollbar error report with random timestamps, levels, messages, and counts for testing and demos.
Python code
34 linesimport json
import random
import time
from datetime import datetime, timedelta
def mock_rollbar_report(n_errors=5):
messages = [
"TypeError: unsupported operand type(s) for +: 'int' and 'str'",
"KeyError: 'user_id'",
"ValueError: invalid literal for int() with base 10: 'abc'",
"AttributeError: 'NoneType' object has no attribute 'split'",
"IndexError: list index out of range",
"FileNotFoundError: [Errno 2] No such file or directory: 'data.csv'",
"ZeroDivisionError: division by zero",
"ImportError: No module named 'requests'",
]
report = []
now = datetime.utcnow()
for i in range(n_errors):
report.append({
"timestamp": (now - timedelta(minutes=random.randint(0, 120))).isoformat() + "Z",
"level": random.choice(["error", "critical", "warning"]),
"message": random.choice(messages),
"code": random.choice(["E001", "E002", "E003"]),
"count": random.randint(1, 100),
"environment": random.choice(["production", "staging", "development"]),
})
return report
if __name__ == "__main__":
report = mock_rollbar_report(3)
print(json.dumps(report, indent=2))
Output
[
{
"timestamp": "2024-01-15T10:23:45.123456Z",
"level": "error",
"message": "KeyError: 'user_id'",
"code": "E002",
"count": 42,
"environment": "production"
},
{
"timestamp": "2024-01-15T09:15:30.654321Z",
"level": "warning",
"message": "FileNotFoundError: [Errno 2] No such file or directory: 'data.csv'",
"code": "E001",
"count": 7,
"environment": "staging"
},
{
"timestamp": "2024-01-15T08:30:10.987654Z",
"level": "critical",
"message": "ZeroDivisionError: division by zero",
"code": "E003",
"count": 89,
"environment": "development"
}
]
How it works
This function builds a list of dictionaries that mimic a Rollbar error report. It uses datetime.utcnow() as the reference point and subtracts random minutes to create realistic timestamps in ISO 8601 format with a 'Z' suffix for UTC. Random selection from predefined messages, levels, codes, counts, and environments adds variety while keeping the output plausible. The json.dumps call with indent=2 produces a readable, formatted JSON array that matches the structure of real Rollbar API responses.
Common mistakes
- Forgetting the 'Z' suffix when generating ISO timestamps for UTC time
- Using `random.choice` without importing `random` explicitly
- Hardcoding timestamps instead of generating them relative to now
Variations
- Use `datetime.now(timezone.utc)` with `isoformat(timespec='milliseconds')` for higher precision
- Accept a `seed` parameter and call `random.seed(seed)` for reproducible outputs
Real-world use cases
- Testing error-reporting dashboards without polluting production Rollbar with real errors.
- Generating demo data for engineering blog posts or internal tooling walkthroughs.
- Feeding mock error streams into alerting systems to verify notification pipelines and Slack integrations.
Sponsored
More from Modern tooling
- Build a Recipe Runner Mock in Python easy
- Build a Textual TUI App Skeleton in Python easy
- Configure ruff linter rules in pyproject.toml with Python easy
- Data Conversion Helper Functions in Python easy
- How to Bind and Mock structlog Context in Python medium
- How to Build a Chainable Filter Helper in Python easy
Keep learning
Related tutorials and quizzes for this topic.