AI-Powered Laravel Debugger & Error Handling #Laravel #PHP #AIDebugging

Day 6: Auto-Categorizing Laravel Errors with AI-Powered Tagging

In Day 5, we added filters to search through our AI-powered Laravel debugger logs. Today, we’ll auto-categorize errors using AI into meaningful tags like Database, Validation, Authentication, Blade, etc., and store them with each log for better filtering and analytics.


Step 1: Update the Migration to Add Category Field

If you already ran the migration, create a new one to add the column:

php artisan make:migration add_category_to_ai_debug_logs_table

In the migration file:

public function up()
{
    Schema::table('a_i_debug_logs', function (Blueprint $table) {
        $table->string('category')->nullable()->after('ai_suggestion');
    });
}

Then run:

php artisan migrate

Step 2: Update the AI Debugger Service to Suggest a Category

Modify app/Services/AIDebuggerService.php:

public function categorizeError($errorMessage)
{
    $prompt = "Categorize this Laravel error into one of the following: Database, Authentication, Authorization, Blade, Validation, Routing, FileSystem, Other.\nError: $errorMessage\nCategory:";

    $response = Http::withHeaders([
        'Authorization' => 'Bearer ' . $this->apiKey,
        'Content-Type' => 'application/json',
    ])->post('https://api.openai.com/v1/completions', [
        'model' => 'gpt-4',
        'prompt' => $prompt,
        'max_tokens' => 10,
        'temperature' => 0,
    ]);

    return trim($response->json()['choices'][0]['text'] ?? 'Other');
}

Step 3: Store the Category in the Log Listener

In app/Listeners/LogErrorListener.php, update the handler:

public function handle(MessageLogged $event)
{
    if ($event->level === 'error') {
        $error = $event->message;
        $suggestion = $this->aiDebugger->analyzeError($error);
        $category = $this->aiDebugger->categorizeError($error);

        \Log::channel('ai_debugger')->error("[$category] $error | Suggestion: $suggestion");
        \Log::channel('slack_ai')->error("AI Debugger\nError: $error\nCategory: $category\nSuggestion: $suggestion");

        AIDebugLog::create([
            'error_message' => $error,
            'ai_suggestion' => $suggestion,
            'category' => $category,
        ]);
    }
}

Step 4: Show Category on the Dashboard

In resources/views/ai-debugger/index.blade.php, add a new table column:

<th>Category</th>

And inside the loop:

<td>{{ $log->category ?? 'Uncategorized' }}</td>

Step 5: Add Filter by Category

Extend your filter form above the table:

<div class="col-md-3">
    <select name="category" class="form-select">
        <option value="">All Categories</option>
        @foreach(['Database', 'Authentication', 'Authorization', 'Blade', 'Validation', 'Routing', 'FileSystem', 'Other'] as $cat)
            <option value="{{ $cat }}" {{ request('category') === $cat ? 'selected' : '' }}>
                {{ $cat }}
            </option>
        @endforeach
    </select>
</div>

And update the controller:

if ($request->filled('category')) {
    $query->where('category', $request->category);
}

Step 6: Test It

  1. Trigger different types of Laravel errors.
  2. Visit /ai-debugger and verify:
    • Each log has a category.
    • The category shows up in the table.
    • Filtering by category works.
See also  Best Practices for Using php artisan migrate in Laravel

Next Steps

In Day 7, we’ll allow manual overrides and tagging of AI categories from the dashboard and store revision history for auditing/debugging purposes.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.