Day 7 – Translating Audit Logs into Business English Using GPT #LaravelGPTAudit #BusinessFriendlyLogs #AIReadableAudit #LaravelAI


Today we’ll make audit logs easier for non-technical users (e.g. managers, compliance officers) by having GPT convert system-generated log entries into plain business English — turning “User ID 14 deleted Record 95” into something like:

“Alex Tan removed the 12-month renewal package for Toyota Hilux on 3 June.”


🧠 Step 1: Add a plain-English formatter

Update your helper: AuditSummaryHelper.php

public static function translateToPlainEnglish(array $logs): string
{
    $logLines = collect($logs)->map(function ($log) {
        $user = optional($log->causer)->name ?? 'System';
        $model = class_basename($log->subject_type ?? 'Unknown');
        $id = $log->subject_id ?? 'N/A';
        $desc = $log->description ?? 'Performed action';
        $time = $log->created_at->format('Y-m-d H:i');

        return "- $time | $user | $desc | $model ID $id";
    })->implode("\n");

    $prompt = <<<PROMPT
You are a Laravel audit assistant. Convert the following technical logs into clear, non-technical, business-friendly language. Mention model names, users, actions, and dates.

$logLines

Plain English Summary:
PROMPT;

    $response = \OpenAI\Laravel\Facades\OpenAI::chat()->create([
        'model' => 'gpt-4o',
        'messages' => [
            ['role' => 'user', 'content' => $prompt],
        ],
        'max_tokens' => 500,
    ]);

    return $response->choices[0]->message->content ?? 'No plain summary generated.';
}

📡 Step 2: Add controller and route to trigger plain summary

In web.php:

Route::post('/audit-logs/plain-summary', [\App\Http\Controllers\AuditLogController::class, 'plainSummary'])->name('audit.logs.plain.summary');

In AuditLogController.php:

public function plainSummary(Request $request)
{
    $logs = \Spatie\Activitylog\Models\Activity::with('causer')->latest()->take(30)->get();
    $plainSummary = \App\Helpers\AuditSummaryHelper::translateToPlainEnglish($logs);

    return back()->with('plain_summary', $plainSummary);
}

🖱 Step 3: Add button and summary output to view

In audit_logs/index.blade.php:

<form method="POST" action="{{ route('audit.logs.plain.summary') }}" class="mb-4 inline-block">
    @csrf
    <button class="bg-yellow-500 text-white px-3 py-1 rounded">Translate to Plain English</button>
</form>

@if(session('plain_summary'))
    <div class="bg-yellow-100 text-yellow-900 p-3 rounded mb-4 whitespace-pre-wrap">
        <strong>Business-Friendly Summary:</strong><br>
        {!! nl2br(e(session('plain_summary'))) !!}
    </div>
@endif

💡 Example Output

Business-Friendly Summary:
On 4 June, Admin updated the end date of Vehicle Plan ID 32.
Jane Lee deleted an expired package for Customer ID 47 at 2:05 PM.
System performed 3 automatic renewals for customer accounts during the day.


✅ Tomorrow (Day 8), we’ll allow admin users to ask natural language questions like “What changed yesterday?” using GPT + function calling to query logs dynamically.

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.