Skip to content

bmadigan/laravel-ai-log-analysis

Repository files navigation

Laravel Log Analysis AI

An AI-powered log analysis system that automatically monitors, vectorizes, and analyzes Laravel application logs to detect incidents and assess severity levels.

Log Analysis AI Dashboard

Features

  • Real-time Log Monitoring: MCP server integration for continuous log file watching
  • AI-Powered Analysis: Uses LLMs (Claude, GPT, etc.) via Prism to analyze log entries
  • Semantic Search: Vector embeddings enable similarity-based log grouping
  • Incident Detection: Automatic severity classification (low, medium, high, critical)
  • Critical Alert System: Dismissible banners for critical incidents with viewed status tracking
  • Test Error Generator: One-click generation of 5-10 random test logs with automatic AI analysis
  • Interactive Dashboard: Livewire-powered UI with real-time updates and polling
  • Asynchronous Processing: Queue-based architecture for scalable analysis

Tech Stack

  • Laravel 12: Latest Laravel framework
  • Prism v0.96: Multi-provider LLM integration
  • Overpass v0.7: Python ML bridge for embeddings
  • Livewire 3 + Volt: Interactive, real-time dashboard
  • Laravel MCP v0.3: Model Context Protocol server
  • SQLite: Vector embeddings stored as JSON for future semantic search
  • Tailwind CSS v4: Modern styling with dark mode
  • Sentence Transformers: all-MiniLM-L6-v2 for 384-dim embeddings

Prerequisites

  • PHP 8.2 or higher
  • Composer
  • Node.js & NPM
  • Python 3.9 or higher
  • SQLite 3.x

Installation

  1. Clone the repository

    git clone <repository-url>
    cd LogAnalysisAi
  2. Install PHP dependencies

    composer install
  3. Install Node dependencies

    npm install
  4. Set up environment file

    cp .env.example .env
    php artisan key:generate
  5. Configure database

    The default SQLite database is already configured in .env:

    DB_CONNECTION=sqlite
    DB_DATABASE=database/database.sqlite

    Create the database file:

    touch database/database.sqlite
  6. Run migrations

    php artisan migrate
  7. Install Python dependencies for Overpass

    cd overpass-ai
    pip3 install -r requirements.txt
    cd ..

    Note: The first run will download the sentence-transformers model (~80MB).

Configuration

AI Provider Setup

Configure your preferred LLM provider in .env:

For Anthropic Claude:

LOG_ANALYSIS_PROVIDER=anthropic
LOG_ANALYSIS_MODEL=claude-3-haiku-20240307
ANTHROPIC_API_KEY=your_api_key_here

For OpenAI:

LOG_ANALYSIS_PROVIDER=openai
LOG_ANALYSIS_MODEL=gpt-4-turbo
OPENAI_API_KEY=your_api_key_here

Analysis Parameters

Adjust AI analysis settings:

LOG_ANALYSIS_MAX_TOKENS=200
LOG_ANALYSIS_TEMPERATURE=0.3

Python Bridge Configuration

Overpass is pre-configured to use the overpass-ai directory. Ensure Python 3.9+ is active:

python3 --version  # Should show Python 3.9 or higher

Running the Application

Quick Start (Recommended)

Start all services at once with a single command:

composer run dev

This starts: web server, queue worker, log viewer (Pail), and Vite dev server.

Individual Services

Alternatively, run services separately:

1. Start the Development Server

php artisan serve

2. Start the Queue Worker In a separate terminal:

php artisan queue:work

3. Build Frontend Assets For development:

npm run dev

For production:

npm run build

4. MCP Server (Optional) The MCP server runs automatically with the web server. It's accessible at:

http://loganalysisai.test/mcp/log-watcher

To test it with MCP Inspector:

php artisan mcp:inspector

Usage

Web Dashboard

Visit http://loganalysisai.test (if using Laravel Herd) or http://localhost:8000 to access the interactive dashboard showing:

  • Recent log entries (latest 10)
  • AI-detected incidents (latest 5)
  • Critical alert banners for unviewed incidents
  • Real-time refresh capability (auto-polls every 10 seconds)

Generate Test Errors

The dashboard includes a "Generate Test Errors" button that creates 5-10 random log entries with varying severity levels (critical, error, warning, info, debug). These logs are automatically:

  1. Written to storage/logs/laravel.log
  2. Processed through the analysis pipeline
  3. Vectorized using sentence-transformers
  4. Analyzed by your configured LLM
  5. Displayed in the dashboard with severity classification

This is the easiest way to test the complete AI analysis workflow.

Critical Alerts

When the system detects critical incidents (like disk space warnings or database connection pool exhaustion), they appear as red alert banners at the top of the dashboard. You can dismiss these alerts by clicking the X button, which marks them as "viewed" and prevents them from reappearing.

MCP Integration

The application includes an MCP (Model Context Protocol) server for AI assistant integration. It's available at /mcp/log-watcher and provides:

  • Tools: monitor_logs - Process recent log entries with AI
  • Resources: log_entries - Access analyzed log data

Monitoring Logs

You can manually trigger log analysis:

use App\Actions\LogMonitor;

$monitor = new LogMonitor();
$monitor->handleNewLine('[2024-10-22 12:00:00] production.ERROR: Database connection failed');

Generating Test Data

The best way to test the full analysis pipeline is to generate real log entries:

php artisan tinker
// Generate log entries that will be analyzed by AI
Log::error('Database connection failed');
Log::warning('High memory usage detected: 95%');
Log::error('API rate limit exceeded for endpoint /users');
Log::critical('Payment gateway timeout - transaction failed');
exit

These logs will automatically:

  1. Be detected by the log monitor
  2. Trigger the analysis queue job
  3. Get vectorized via Overpass
  4. Be analyzed by Prism AI
  5. Create incidents visible in the dashboard

Alternatively, use factories to create sample data (bypasses analysis):

// Create log entries without analysis
App\Models\LogEntry::factory()->count(20)->create();

// Create incidents directly
App\Models\Incident::factory()->count(5)->create();

Development Commands

Format code with Laravel Pint:

vendor/bin/pint

Check specific files:

vendor/bin/pint --dirty

Run Tinker REPL:

php artisan tinker

Clear all caches:

php artisan optimize:clear

Project Structure

app/
├── Actions/           # Domain logic classes
│   ├── LogMonitor.php
│   ├── LogVectorizer.php
│   ├── LogAnalyzer.php
│   └── IncidentManager.php
├── Jobs/              # Queue jobs
│   └── AnalyzeLogJob.php
├── Mcp/               # MCP server components
│   ├── Servers/
│   ├── Tools/
│   └── Resources/
└── Models/            # Eloquent models
    ├── LogEntry.php
    ├── LogVector.php
    └── Incident.php

overpass-ai/           # Python ML bridge
├── main.py
├── vectorize.py
└── requirements.txt

resources/views/livewire/  # Volt components
└── log-dashboard.blade.php

Architecture

The application follows a clean architecture pattern:

  1. Log Ingestion: LogMonitor creates LogEntry records
  2. Async Processing: AnalyzeLogJob is dispatched to queue
  3. Vectorization: LogVectorizer generates embeddings via Overpass
  4. AI Analysis: LogAnalyzer uses Prism to assess severity
  5. Incident Creation: IncidentManager stores analysis results
  6. Dashboard Display: Livewire shows real-time insights

Common Issues

Queue not processing

Ensure the queue worker is running:

php artisan queue:work --verbose

Python environment errors

Reinstall Python dependencies:

cd overpass-ai
pip3 install --upgrade -r requirements.txt

If you see "ModuleNotFoundError: No module named 'sentence_transformers'", ensure you've installed the requirements.

LLM API errors

Check your API key configuration and ensure you have credits/quota:

php artisan tinker
>>> config('prism.providers.anthropic.api_key')

Database locked errors

SQLite has limited concurrency. For production, consider PostgreSQL or MySQL.

Tutorial

This project is designed as an advanced tutorial for integrating AI capabilities into Laravel applications. It demonstrates:

  • Multi-provider LLM integration via Prism
  • Python ML bridge with Overpass
  • MCP server implementation
  • Real-time Livewire dashboards
  • Queue-based async processing
  • Active Pattern for domain logic organization

A comprehensive tutorial covering the architecture, patterns, and implementation details is available in .ai/tutorial.md. The tutorial is written for advanced Laravel developers and covers:

  • The Active Pattern and why it's used over traditional service classes
  • Python/PHP bridge architecture with Overpass
  • Vector storage using SQLite JSON columns
  • LLM integration with structured outputs via Prism
  • MCP server implementation for AI accessibility
  • Complete request flow from log ingestion to incident detection

Note: This is an educational project. Automated testing is intentionally omitted to focus on core functionality.

License

This project is open-sourced software licensed under the MIT license.

About

Laravel MCP and RAG Log Analysis Sample App. Uses Laravel MCP Server, Overpass for python bridge and Prism for LLMs

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages