ai_error_tracker

This is an old revision of the document!


AI Error Tracker

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.

Purpose

The AI Error Tracker System is designed to solve specific challenges in modern applications:

  • Error Logging: Maintain a detailed, persistent log of all errors encountered during runtime.
  • Structured Error Analysis: Organize error metadata (e.g., context, severity) for effective debugging.
  • Dynamic Querying: Retrieve errors based on filtering criteria like severity or context.
  • Scalability and Extensibility: Easily adapt to larger systems with more complex error monitoring requirements.

Key focus areas include:

  • Long-Term Error Persistence: Ensuring errors are retained for future analysis.
  • Diagnostic Support: Enabling developers to isolate and address recurring or critical issues.

Key Features

1. SQLite-Based Database for Error Tracking:

  • Uses an embedded SQLite database (errors.db) to log and store all errors encountered.

2. Structured Error Logs:

  • Logs errors with metadata, such as:
    • Timestamp of occurrence
    • Error message details
    • Context (e.g., subsystem where the error occurred)
    • Severity levels (LOW, MEDIUM, HIGH, CRITICAL).

3. Dynamic Querying and Retrieval:

  • Supports retrieving error logs based on filtering criteria, such as severity level.

4. Ease of Integration:

  • Easily integrates into existing codebases with minimal configuration.

5. Extensibility:

  • The database schema and logging mechanisms are designed to be extendable for larger applications.

6. Error Logging Alert System:

  • Error logging automatically records issues and can be extended to generate alerts in critical cases.

Architecture

The ErrorTracker class is responsible for:

  • Initializing the SQLite database schema.
  • Recording structured logs into the database.
  • Querying errors dynamically based on severity or other criteria.

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 []

Usage Examples

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()
    )

Best Practices

1. Persist Logs Securely:

  1. Regularly back up the SQLite database or use a centralized logging system in production.

2. Use Severity Levels Appropriately:

  1. Assign severity levels (`LOW`, `MEDIUM`, `HIGH`, `CRITICAL`) to prioritize debugging.

3. Extend for Specialized Use Cases:

  1. Include additional metadata fields, such as stack traces, user IDs, or application states.

4. Monitor and Analyze Trends:

  1. Periodically analyze logged errors to uncover common problem areas or recurring bugs.

5. Integrate with Monitoring Tools:

  1. Pair with external alerting tools, such as email notifications for critical errors.

Conclusion

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.

ai_error_tracker.1748302319.txt.gz · Last modified: 2025/05/26 23:31 by eagleeyenebula