More Developers Docs: The AI Error Tracker System is a robust framework for logging, monitoring, and analyzing application errors. It integrates with a SQLite database to keep a structured historical record of errors, including timestamps, messages, severity levels, and specific contexts. This system is designed for long-term error tracking and supports dynamic querying for error analysis.
The ErrorTracker class is at the heart of the system, streamlining error tracking and providing tools for developers to monitor and debug issues effectively.
The AI Error Tracker System is designed to solve specific challenges in modern applications:
Key focus areas include:
1. SQLite-Based Database for Error Tracking:
2. Structured Error Logs:
3. Dynamic Querying and Retrieval:
4. Ease of Integration:
5. Extensibility:
6. Error Logging Alert System:
The ErrorTracker class is responsible for:
Class Overview
python
import sqlite3
import logging
from datetime import datetime
class ErrorTracker:
"""
Logs and tracks errors in a structured database for long-term analysis.
"""
def __init__(self, db_path="errors.db"):
self.db_path = db_path
self._initialize_database()
def _initialize_database(self):
"""
Initializes the SQLite database for error tracking.
Creates a table if it does not exist.
"""
try:
with sqlite3.connect(self.db_path) as conn:
cursor = conn.cursor()
cursor.execute('''CREATE TABLE IF NOT EXISTS errors (
id INTEGER PRIMARY KEY,
timestamp TEXT,
error_message TEXT,
context TEXT,
severity TEXT
)''')
conn.commit()
except Exception as e:
logging.error(f"Failed to initialize error database: {e}")
def log_error(self, error_message, context=None, severity="LOW"):
"""
Logs an error into the database.
:param error_message: Description of the error
:param context: The section where the error occurred (optional)
:param severity: Severity level (default: LOW)
"""
timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
try:
with sqlite3.connect(self.db_path) as conn:
cursor = conn.cursor()
cursor.execute('''INSERT INTO errors (timestamp, error_message, context, severity)
VALUES (?, ?, ?, ?)''', (timestamp, error_message, context, severity))
conn.commit()
logging.info(f"Error logged: {error_message}")
except Exception as e:
logging.error(f"Failed to log error: {e}")
def fetch_errors(self, severity=None):
"""
Retrieves logged errors based on optional severity filtering.
:param severity: Severity level to filter by (optional)
:return: List of error records
"""
try:
with sqlite3.connect(self.db_path) as conn:
cursor = conn.cursor()
query = "SELECT * FROM errors"
params = []
if severity:
query += " WHERE severity = ?"
params.append(severity)
cursor.execute(query, params)
return cursor.fetchall()
except Exception as e:
logging.error(f"Failed to fetch errors: {e}")
return []
Here are advanced usage examples for leveraging the Error Tracker in real-world applications, including basic error logging, filtering, and extending the system.
Example 1: Logging and Viewing Errors
Log a simple error into the system and view the resulting logs.
python from ai_error_tracker import ErrorTracker
Initialize the Error Tracker
error_tracker = ErrorTracker()
Log an error
error_tracker.log_error(
error_message="Failed to connect to the database.",
context="Database Connection",
severity="CRITICAL"
)
Fetch and print all errors
errors = error_tracker.fetch_errors()
for error in errors:
print(error)
Logs & Output Example:
(1, '2023-11-05 12:30:21', 'Failed to connect to the database.', 'Database Connection', 'CRITICAL')
Example 2: Filtering Errors by Severity
* Retrieve errors based on their severity level to prioritize debugging efforts.
python
Log a few example errors
error_tracker.log_error("Warning: Deprecated API used.", "API Module", "MEDIUM")
error_tracker.log_error("File not found during upload.", "File Upload", "LOW")
Fetch errors with severity MEDIUM
medium_severity_errors = error_tracker.fetch_errors(severity="MEDIUM")
print("MEDIUM Severity Errors:")
for error in medium_severity_errors:
print(error)
Logs & Output Example:
MEDIUM Severity Errors: (2, '2023-11-05 12:35:11', 'Warning: Deprecated API used.', 'API Module', 'MEDIUM')
Example 3: Integrating into AI Pipelines
The system integrates seamlessly into AI workflows to log pipeline errors dynamically.
python
Simulate a pipeline with error handling
def execute_pipeline():
# Simulating a failure
raise RuntimeError("Invalid data format in pipeline.")
Capture pipeline errors and log them
try:
execute_pipeline()
except Exception as e:
error_tracker.log_error(
error_message=str(e),
context="Pipeline Execution",
severity="CRITICAL"
)
Logs & Output Example (Database):
(3, '2023-11-05 12:40:55', 'Invalid data format in pipeline.', 'Pipeline Execution', 'CRITICAL')
Example 4: Adding Custom Error Handling Attributes * You can extend the system to include additional fields for improved context.
python
class AdvancedErrorTracker(ErrorTracker):
def log_advanced_error(self, error_message, context=None, severity="LOW", stack_trace=None):
"""
Log an error with a stack trace for debugging.
"""
timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
try:
with sqlite3.connect(self.db_path) as conn:
cursor = conn.cursor()
cursor.execute('''INSERT INTO errors (timestamp, error_message, context, severity)
VALUES (?, ?, ?, ?)''', (timestamp, error_message, context, severity))
conn.commit()
if stack_trace:
logging.info(f"Stack Trace: {stack_trace}")
logging.info(f"Advanced error logged: {error_message}")
except Exception as e:
logging.error(f"Failed to log advanced error: {e}")
Example usage of extended tracker
advanced_tracker = AdvancedErrorTracker()
try:
execute_pipeline()
except Exception as e:
import traceback
advanced_tracker.log_advanced_error(
error_message=str(e),
context="Pipeline Execution",
severity="CRITICAL",
stack_trace=traceback.format_exc()
)
1. Persist Logs Securely:
2. Use Severity Levels Appropriately:
3. Extend for Specialized Use Cases:
4. Monitor and Analyze Trends:
5. Integrate with Monitoring Tools:
The AI Error Tracker System provides a structured, scalable, and easy-to-use framework for logging and analyzing application errors. Its extensible design and rich feature set make it a valuable tool for modern software workflows, supporting everything from debugging to long-term trend analysis. With features like severity-based filtering and dynamic querying, the ErrorTracker simplifies error management while offering flexibility for complex use cases.