Day 10 – Logging GPT Usage & Finalizing Your AI-Powered Audit Tool#LaravelGPTAudit #AuditSecurity #GPTLogging #ProductionReadyAI #LaravelCompliance


Today, we wrap up the LaravelGPTAudit series by logging GPT usage, securing access, and preparing your audit tools for production deployment.

You’ll ensure full traceability, prevent abuse, and make your GPT integration audit-compliant.


🧩 Step 1: Log all GPT queries and responses

Create a table:

php artisan make:migration create_gpt_audit_logs_table
Schema::create('gpt_audit_logs', function (Blueprint $table) {
    $table->id();
    $table->foreignId('user_id')->nullable()->constrained()->nullOnDelete();
    $table->string('source')->default('audit_logs');
    $table->text('question');
    $table->longText('response');
    $table->timestamps();
});

Then migrate:

php artisan migrate

🧠 Step 2: Add logging in AuditQueryAssistant

In runQuery() or ask():

use App\Models\GptAuditLog;

GptAuditLog::create([
    'user_id' => auth()->id(),
    'source' => 'audit_logs',
    'question' => $userInput,
    'response' => $summaryOrResult,
]);

Create the model:

php artisan make:model GptAuditLog

🔐 Step 3: Restrict GPT access to authorized users

In AuditLogController.php:

public function ask(Request $request)
{
    abort_unless(auth()->user()?->can('use-gpt-audit'), 403);

    $question = $request->input('question');
    $response = \App\Helpers\AuditQueryAssistant::ask($question);

    return back()->with('audit_query_response', $response);
}

Add a gate or permission in AuthServiceProvider:

Gate::define('use-gpt-audit', function ($user) {
    return $user->hasRole('admin') || $user->hasPermission('audit-gpt');
});

🧪 Step 4: Monitor GPT usage

Add a view like resources/views/gpt_logs/index.blade.php:

<table class="table-auto w-full border">
    <thead>
        <tr>
            <th>User</th>
            <th>Question</th>
            <th>Response</th>
            <th>Time</th>
        </tr>
    </thead>
    <tbody>
        @foreach ($logs as $log)
            <tr>
                <td>{{ optional($log->user)->name ?? 'System' }}</td>
                <td>{{ $log->question }}</td>
                <td class="whitespace-pre-wrap">{{ Str::limit($log->response, 300) }}</td>
                <td>{{ $log->created_at->format('Y-m-d H:i') }}</td>
            </tr>
        @endforeach
    </tbody>
</table>

📦 Bonus: Rate limit GPT calls (optional)

Use Laravel rate limiting in controller:

use Illuminate\Support\Facades\RateLimiter;

$tooMany = !RateLimiter::attempt(
    'gpt-audit:' . auth()->id(),
    $perMinute = 5
);

if ($tooMany) {
    return back()->withErrors(['Too many GPT queries. Please wait.']);
}

✅ You now have:

✅ Activity logs
✅ Human-friendly summaries
✅ Smart filters
✅ Anomaly detection
✅ Role breakdown
✅ Weekly reports
✅ Natural query interface
✅ GPT access logging
✅ Access control

See also  Day 6 – Automating Weekly Audit Reports with GPT and Email #LaravelGPTAudit #AuditAutomation #LaravelScheduler #AIAuditReports

🎉 Congratulations — your Laravel AI-powered audit system is now production-ready.

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.