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

Day 2: Capturing Laravel Logs and Sending Errors to AI Debugger

In Day 1, we set up an AI-powered Laravel debugger using OpenAI to analyze errors. Today, we’ll automatically capture Laravel logs and send errors to our AI debugger for real-time analysis.


Step 1: Configure Laravel Logging

Laravel uses Monolog for logging. By default, errors are stored in storage/logs/laravel.log. We’ll customize this behavior to automatically send logs to our AI Debugger.

1. Create a Custom Log Channel

Open config/logging.php and add a new custom channel inside the channels array:

'channels' => [
    // Default channels...

    'ai_debugger' => [
        'driver' => 'single',
        'path' => storage_path('logs/ai_debugger.log'),
        'level' => 'error',
    ],
],

Now, Laravel will log errors separately in storage/logs/ai_debugger.log.


Step 2: Create a Custom Log Handler

To automatically send errors to AI Debugger, we’ll create a custom Laravel log handler.

1. Create a Log Listener

Run:

php artisan make:listener LogErrorListener

Open app/Listeners/LogErrorListener.php and modify:

namespace App\Listeners;

use Illuminate\Log\Events\MessageLogged;
use App\Services\AIDebuggerService;
use Illuminate\Contracts\Queue\ShouldQueue;

class LogErrorListener implements ShouldQueue
{
    protected $aiDebugger;

    public function __construct(AIDebuggerService $aiDebugger)
    {
        $this->aiDebugger = $aiDebugger;
    }

    public function handle(MessageLogged $event)
    {
        if ($event->level === 'error') {
            $suggestion = $this->aiDebugger->analyzeError($event->message);
            \Log::channel('ai_debugger')->error('AI Suggestion: ' . $suggestion);
        }
    }
}

Step 3: Register the Listener

Open app/Providers/EventServiceProvider.php and register the listener inside $listen:

protected $listen = [
    'Illuminate\Log\Events\MessageLogged' => [
        'App\Listeners\LogErrorListener',
    ],
];

Run:

php artisan event:clear
php artisan event:cache

Step 4: Test Automatic AI Debugging

Now, let’s trigger an error manually in Laravel and check if the AI debugger logs a suggestion.

See also  A Comprehensive Guide to PHP Laravel with Alpine.js

1. Create a Test Route

Open routes/web.php and add:

Route::get('/test-error', function () {
    \Log::error('SQLSTATE[HY000]: General error: 1364 Field doesn\'t have a default value');
    return 'Error logged!';
});

2. Check AI Debugging Logs

Run Laravel:

php artisan serve

Visit http://127.0.0.1:8000/test-error. Then check:

cat storage/logs/ai_debugger.log

Example log:

[2025-03-20 12:00:00] AI Suggestion: This error occurs when a column is set to NOT NULL but doesn't have a default value. Check your migration and set a default value using $table->string('column')->default('value').

Next Steps

  • In Day 3, we’ll send AI debugging results via email and integrate Slack notifications.
  • Stay tuned for smarter Laravel error handling with AI! 🚀

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.