跪拜 Guibai
← All articles
Backend · AIGC · Automated Operations

LightLog Replaces the ELK Stack with PostgreSQL and Adds an AI Troubleshooting Agent

By 云边有个技术书屋 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Teams running small-to-medium services often face a bad tradeoff: ELK is too heavy, Loki's full-text search is too weak, and SaaS log tools get expensive fast. LightLog shows that PostgreSQL, with the right schema and index choices, handles log workloads well enough to skip dedicated search engines entirely, while the built-in AI agent turns log diving from a grep slog into a conversation.

Summary

LightLog is a new open-source log platform built to sidestep the resource and ops burden of Elasticsearch. It stores logs in PostgreSQL using daily partitions, BRIN indexes for time-series data, and JSONB with GIN indexes for semi-structured fields, returning results from 30 million rows in 34 milliseconds. Storage is pluggable across SQLite, H2, and PG via an environment variable.

Integration requires adding one Maven dependency and four lines of YAML to a Spring Boot service; a custom Logback appender ships logs asynchronously through an in-memory queue to a write-ahead log on disk before pushing to the central HTTP API, so business threads never block on network calls. The WAL replays on restart, preventing log loss during outages.

An AI assistant built on LangGraph and litellm exposes four hard-limited tools—search, count, list services, and recent snapshot—so operators can ask questions like "what errors did ths-api have today?" and get a fingerprint analysis without burning tokens on unbounded context windows.

Takeaways
PostgreSQL with daily partitions, BRIN indexes, and JSONB GIN indexes returned 34ms query results on 30 million log rows.
Storage is pluggable: SQLite for local dev, H2 for tests, PG for production, switched by environment variable.
Java integration needs only a Maven dependency and four YAML lines; no code changes required, and it works across Java 8/17 and Spring Boot 2.x/3.x.
An async Logback appender writes to an in-memory queue, then to a WAL file on disk before HTTP push, so business threads never wait on the network.
The WAL replays on process restart, pushing any unpushed files before new logs enter the queue.
The AI agent uses LangGraph with a hard recursion limit of 10 and four tools, each capped to prevent token cost explosions.
litellm lets the agent switch between LLM providers (Qwen, GPT-4o, Claude, DeepSeek) by changing one model string.
Old log partitions are dropped in milliseconds with DROP PARTITION, avoiding DELETE bloat and VACUUM overhead.
Conclusions

Hard-limiting the AI agent's search tool to 100 log entries is framed as an economic decision, not a technical one—stuffing 1,000 entries into an LLM context could cost ¥0.50 per call, and 100 entries are enough to spot patterns.

The WAL design is explicitly justified by user trust: once logs go missing, confidence in the system collapses, so durability is treated as a minimum promise rather than over-engineering.

BRIN indexes are a pragmatic fit for append-only log data because they store only block-level min/max values, shrinking index size to roughly 1/100th of a B-tree, though they trade off some scan precision.

Pluggable storage via a Spring factory pattern and environment variable means the same codebase runs with zero external dependencies in development (SQLite) and scales to PG in production without config rewrites.

Concepts & terms
BRIN Index
Block Range Index in PostgreSQL. Instead of indexing every row like a B-tree, it stores only the minimum and maximum values for each block of pages, making it extremely small and fast for naturally ordered data like append-only timestamps.
WAL (Write-Ahead Log)
A durability pattern where data is written to a persistent log file on disk before being sent to a remote service. If the remote push fails, the file remains and is retried, preventing data loss during network outages or crashes.
LangGraph
LangChain's agent framework that models AI workflows as explicit state machines with nodes (functions) and edges (transition rules), giving developers hard control over loop limits and visibility into each step of a tool-calling agent.
litellm
A library that provides a unified interface to call multiple LLM providers (OpenAI, Anthropic, DeepSeek, Qwen, etc.) by changing a single model string, abstracting away API differences.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗