Production infrastructure for AI agents
Website · Documentation · Guides · Core · Template · Discord
systemprompt-logging
Governance you can prove needs a record no one can quietly edit. This crate writes every request, AI call, and tool execution to a tracing pipeline that lands in your PostgreSQL as a queryable audit trail, then hands you typed queries over it.
Layer: Infra. Infrastructure primitives consumed by the domain and application crates. Part of the systemprompt-core workspace.
What it does
A tracing layer batches structured events into PostgreSQL asynchronously, propagating context (user_id, session_id, task_id, trace_id) onto every row so the audit trail reconstructs who did what. A cron scheduler applies per-level retention. On top of the stored trail sits a typed query surface over traces, AI requests, and MCP tool executions. The cli feature adds console formatting for the CLI.
Modules
| Module | Purpose |
|---|---|
layer |
DatabaseLayer (async batch persistence) and ProxyDatabaseLayer (late-bound sink swap-in), plus field/span visitors. |
models |
LogEntry, LogLevel, LogFilter, LogActor, the LoggingError type, and the LogRow SQLx mapping. |
repository |
LoggingRepository (log CRUD) and AnalyticsRepository / AnalyticsEvent. |
services |
DatabaseLogService, LoggingMaintenanceService, retention scheduling, request/system spans, startup-mode and log-publisher control, and the cli/ display helpers (feature-gated). |
trace |
TraceQueryService and AiTraceService with the typed rows for traces, AI requests, and MCP tool executions. |
attribution |
Installs the process-wide system-actor attribution used to stamp platform-originated log rows (install_log_attribution, platform_attribution). |
sanitize |
Internal helpers that scrub sensitive fields before rows are persisted. |
extension |
LoggingExtension registers the logs and analytics_events schema through the workspace extension framework. |
The cli submodule under services/ provides banners, display, macros, service (CliService), startup, table, theme, and types. Schema DDL lives in schema/ (log.sql, analytics.sql, and the migrations/ directory).
Usage
[]
= "0.21"
use DbPool;
use ;
use LoggingError;
async
Public API
Initialization
init_logging(db_pool)- Initialize with database persistenceinit_console_logging()- Initialize console-only logginginit_console_logging_with_level(level)- Initialize console logging at a specific levelLoggingExtension- Inventory-registered schema/extension entry point
Core Types
LogEntry- Log entry with metadata and context IDsLogLevel- Enum: ERROR, WARN, INFO, DEBUG, TRACELogFilter- Pagination and filtering for queriesLoggingError- Error type for logging operationsDatabaseLayer- Tracing layer for async database persistence
Repositories
LoggingRepository- CRUD operations for logsAnalyticsRepository- Analytics event trackingAnalyticsEvent- Structured analytics event
Services
DatabaseLogService- ImplementsLogServicetrait from systemprompt-traitsLoggingMaintenanceService- Log cleanup and maintenanceCliService- Rich CLI output facadeFilterSystemFields- Console field filter
Spans
RequestSpan- For user-initiated operationsSystemSpan- For internal/background operationsRequestSpanBuilder- Fluent builder for request spans
Retention
RetentionConfig- Per-level retention configurationRetentionPolicy- Individual retention policyRetentionScheduler- Cron-based cleanup scheduler
Trace Services
TraceQueryService- Generic trace queryingAiTraceService- AI/MCP operation tracingTraceEvent,TaskInfo,ExecutionStep,AiRequestInfoMcpToolExecution,ConversationMessage,ToolLogEntry
Output Control
is_startup_mode(),set_startup_mode()- Startup mode controlpublish_log(),set_log_publisher()- Log event publishing
Attribution
install_log_attribution(),platform_attribution()- Process-wide system-actor stamping for platform-originated rows
Feature Flags
| Feature | Dependencies enabled |
|---|---|
cli |
console, indicatif |
Dependencies
Internal
systemprompt-database- Database pool managementsystemprompt-extension- Extension framework (schema registration)systemprompt-identifiers- Typed identifiers (UserId, SessionId, etc.)systemprompt-models- Shared model typessystemprompt-traits- Shared trait definitions (LogService)
External
tracing,tracing-subscriber- Structured logging frameworktokio- Async runtimesqlx- Type-safe SQLserde,serde_json,serde_yaml- Serialisationchrono- Timestamp handlinguuid- Log entry identifierstokio-cron-scheduler- Retention job schedulingasync-trait,thiserror,inventory- Trait, error, and registration utilitiesconsole,indicatif- CLI utilities (clifeature)
License
BSL-1.1 (Business Source License). Source-available for evaluation, testing, and non-production use. Production use requires a commercial license. Each version converts to Apache 2.0 four years after publication. See LICENSE.
systemprompt.io · Documentation · Guides · Live Demo · Template · crates.io · docs.rs · Discord
Infra layer · Own how your organization uses AI.