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

Day 10: Finalizing, Exporting Logs, and Making the AI Debugger Production-Ready

In Day 9, we implemented a feedback system to evaluate AI suggestions. Today, we’ll wrap up the series by making your AI-powered Laravel debugger production-ready with log exporting, permission control, and code cleanup.


Step 1: Add Export to CSV

1. Define a Route

In routes/web.php:

Route::get('/ai-debugger/export', [AIDebugLogController::class, 'export'])->name('ai-debugger.export');

2. Add Export Method

In AIDebugLogController.php:

use Symfony\Component\HttpFoundation\StreamedResponse;

public function export()
{
    $logs = AIDebugLog::all();

    $headers = [
        "Content-type" => "text/csv",
        "Content-Disposition" => "attachment; filename=ai_debug_logs.csv",
        "Pragma" => "no-cache",
        "Cache-Control" => "must-revalidate, post-check=0, pre-check=0",
        "Expires" => "0"
    ];

    $columns = ['ID', 'Error Message', 'Suggestion', 'Category', 'Helpful', 'Timestamp'];

    $callback = function () use ($logs, $columns) {
        $file = fopen('php://output', 'w');
        fputcsv($file, $columns);

        foreach ($logs as $log) {
            fputcsv($file, [
                $log->id,
                $log->error_message,
                $log->ai_suggestion,
                $log->category,
                $log->feedback_helpful === null ? 'N/A' : ($log->feedback_helpful ? 'Yes' : 'No'),
                $log->created_at,
            ]);
        }

        fclose($file);
    };

    return response()->stream($callback, 200, $headers);
}

3. Add Export Button in the Dashboard

In the Blade view above the table:

<a href="{{ route('ai-debugger.export') }}" class="btn btn-outline-dark mb-3">📤 Export Logs to CSV</a>

Step 2: Protect the Debugger with Auth Middleware

In routes/web.php, wrap debugger routes:

Route::middleware(['auth'])->group(function () {
    Route::get('/ai-debugger', [AIDebugLogController::class, 'index'])->name('ai-debugger.index');
    Route::put('/ai-debugger/{log}', [AIDebugLogController::class, 'update'])->name('ai-debugger.update');
    Route::post('/ai-debugger/{log}/feedback', [AIDebugLogController::class, 'feedback'])->name('ai-debugger.feedback');
    Route::get('/ai-debugger/export', [AIDebugLogController::class, 'export'])->name('ai-debugger.export');
});

Step 3: Optional – Add Gate or Role-Based Access

In AuthServiceProvider.php:

Gate::define('view-ai-debugger', function ($user) {
    return $user->isAdmin(); // or customize as needed
});

In your controller constructor:

public function __construct()
{
    $this->middleware(function ($request, $next) {
        abort_unless(auth()->user()?->can('view-ai-debugger'), 403);
        return $next($request);
    });
}

Step 4: Code Cleanup Tips

  • Move hardcoded category lists to a config file: config/ai-debugger.php
  • Make feedback and suggestion components Blade components for reuse
  • Add unit tests for the AIDebuggerService and controller methods
  • Queue AI requests using ShouldQueue for better performance on production
See also  What and how to access PHP laravel app after development/deployment URL

Step 5: Deployment Checklist

.env contains valid OPENAI_API_KEY
✅ Authentication & Authorization are enabled
✅ Daily or weekly export scheduled via Task Scheduling
✅ Logging to Slack and email notifications verified
✅ Optional: Connect to your team’s bug tracker via API (e.g., Jira, GitHub Issues)


✅ AI-Powered Laravel Debugger Complete

You’ve just built a production-ready AI debugging system that:

  • Captures Laravel errors
  • Auto-suggests fixes using GPT
  • Notifies your team via email/Slack
  • Logs and tracks feedback
  • Stores revisions and exports data

Use this to speed up debugging, train your team, or even fine-tune your AI prompts for internal LLMs.

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.