systemprompt_cli/commands/infrastructure/logs/
mod.rs1mod audit;
11mod cleanup;
12pub(super) mod delete;
13pub mod duration;
14mod export;
15pub mod request;
16mod search;
17pub mod shared;
18mod show;
19mod stream;
20mod summary;
21pub mod tools;
22pub mod trace;
23pub mod types;
24mod view;
25
26pub use audit::{AuditOutput, AuditToolCall, build_audit, not_found_output as audit_not_found};
27pub use shared::{
28 cost_microdollars_to_dollars, display_log_row, format_optional_duration_ms, format_timestamp,
29};
30pub use summary::{LogsSummaryOutput, build_logs_summary};
31pub use types::{MessageRow, ToolCallRow};
32
33use anyhow::{Result, bail};
34use clap::Subcommand;
35use schemars::JsonSchema;
36use serde::{Deserialize, Serialize};
37use systemprompt_identifiers::{LogId, TraceId};
38
39use crate::context::CommandContext;
40use crate::shared::render_result;
41
42#[derive(Debug, Subcommand)]
43pub enum LogsCommands {
44 #[command(
45 about = "View log entries",
46 after_help = "EXAMPLES:\n systemprompt infra logs view --tail 20\n systemprompt infra \
47 logs view --level error\n systemprompt infra logs view --since 1h"
48 )]
49 View(view::ViewArgs),
50
51 #[command(
52 about = "Search logs by pattern",
53 after_help = "EXAMPLES:\n systemprompt infra logs search \"error\"\n systemprompt infra \
54 logs search \"timeout\" --level error --since 1h"
55 )]
56 Search(search::SearchArgs),
57
58 #[command(
59 about = "Stream logs in real-time (like tail -f)",
60 visible_alias = "follow",
61 after_help = "EXAMPLES:\n systemprompt infra logs stream\n systemprompt infra logs \
62 stream --level error --module agent\n systemprompt infra logs follow"
63 )]
64 Stream(stream::StreamArgs),
65
66 #[command(
67 about = "Export logs to file",
68 after_help = "EXAMPLES:\n systemprompt infra logs export --format json --since 24h\n \
69 systemprompt infra logs export --format csv -o logs.csv"
70 )]
71 Export(export::ExportArgs),
72
73 #[command(about = "Clean up old log entries")]
74 Cleanup(cleanup::CleanupArgs),
75
76 #[command(about = "Delete all log entries")]
77 Delete(delete::DeleteArgs),
78
79 #[command(
80 about = "Show logs summary statistics",
81 after_help = "EXAMPLES:\n systemprompt infra logs summary\n systemprompt infra logs \
82 summary --since 24h"
83 )]
84 Summary(summary::SummaryArgs),
85
86 #[command(
87 about = "Show details of a log entry or all logs for a trace",
88 after_help = "EXAMPLES:\n systemprompt infra logs show log_abc123\n systemprompt infra \
89 logs show trace_def456"
90 )]
91 Show(show::ShowArgs),
92
93 #[command(subcommand, about = "Debug execution traces")]
94 Trace(trace::TraceCommands),
95
96 #[command(subcommand, about = "Inspect AI requests")]
97 Request(request::RequestCommands),
98
99 #[command(subcommand, about = "List and search MCP tool executions")]
100 Tools(tools::ToolsCommands),
101
102 #[command(
103 about = "Full chain reconstruction by request, task, or trace id (identity, policy, prompt/response, tool calls, cost)",
104 after_help = "EXAMPLES:\n systemprompt infra logs audit abc123\n systemprompt infra \
105 logs audit task-xyz"
106 )]
107 Audit(audit::AuditArgs),
108}
109
110#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
111pub struct LogEntryRow {
112 pub id: LogId,
113 pub trace_id: TraceId,
114 pub timestamp: String,
115 pub level: String,
116 pub module: String,
117 pub message: String,
118 #[serde(skip_serializing_if = "Option::is_none")]
119 pub metadata: Option<serde_json::Value>,
120}
121
122#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
123pub struct LogFilters {
124 #[serde(skip_serializing_if = "Option::is_none")]
125 pub level: Option<String>,
126 #[serde(skip_serializing_if = "Option::is_none")]
127 pub module: Option<String>,
128 #[serde(skip_serializing_if = "Option::is_none")]
129 pub since: Option<String>,
130 #[serde(skip_serializing_if = "Option::is_none")]
131 pub pattern: Option<String>,
132 pub tail: i64,
133}
134
135#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
136pub struct LogViewOutput {
137 pub logs: Vec<LogEntryRow>,
138 pub total: u64,
139 pub filters: LogFilters,
140}
141
142#[derive(Debug, Clone, Copy, Serialize, Deserialize, JsonSchema)]
143pub struct LogDeleteOutput {
144 pub deleted_count: u64,
145 pub vacuum_performed: bool,
146}
147
148#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
149pub struct LogCleanupOutput {
150 pub deleted_count: u64,
151 pub dry_run: bool,
152 pub cutoff_date: String,
153 pub vacuum_performed: bool,
154}
155
156#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
157pub struct LogExportOutput {
158 pub exported_count: u64,
159 pub format: String,
160 #[serde(skip_serializing_if = "Option::is_none")]
161 pub file_path: Option<String>,
162}
163
164pub async fn execute(command: LogsCommands, ctx: &CommandContext) -> Result<()> {
165 if ctx.is_database_scoped()
166 && matches!(
167 command,
168 LogsCommands::Stream(_) | LogsCommands::Cleanup(_) | LogsCommands::Delete(_)
169 )
170 {
171 bail!("This logs command requires full profile context");
172 }
173
174 match command {
175 LogsCommands::View(args) => {
176 let result = view::execute(args, ctx).await?;
177 render_result(&result, &ctx.cli);
178 Ok(())
179 },
180 LogsCommands::Search(args) => {
181 let result = search::execute(args, ctx).await?;
182 render_result(&result, &ctx.cli);
183 Ok(())
184 },
185 LogsCommands::Stream(args) => stream::execute(args, ctx).await,
186 LogsCommands::Export(args) => {
187 let result = export::execute(args, ctx).await?;
188 render_result(&result, &ctx.cli);
189 Ok(())
190 },
191 LogsCommands::Cleanup(args) => cleanup::execute(args, ctx).await,
192 LogsCommands::Delete(args) => delete::execute(args, ctx).await,
193 LogsCommands::Summary(args) => summary::execute(args, ctx).await,
194 LogsCommands::Show(args) => show::execute(args, ctx).await,
195 LogsCommands::Trace(cmd) => trace::execute(cmd, ctx).await,
196 LogsCommands::Request(cmd) => request::execute(cmd, ctx).await,
197 LogsCommands::Tools(cmd) => tools::execute(cmd, ctx).await,
198 LogsCommands::Audit(args) => audit::execute(args, ctx).await,
199 }
200}