Skip to main content

systemprompt_cli/commands/infrastructure/logs/
shared.rs

1//! Formatting helpers shared across the `infra logs` subcommands.
2//!
3//! Re-exports the timestamp/duration formatters from `systemprompt_models` and
4//! provides [`display_log_row`] and [`cost_microdollars_to_dollars`] used by
5//! the view, search, and trace renderers.
6//!
7//! Copyright (c) systemprompt.io — Business Source License 1.1.
8//! See <https://systemprompt.io> for licensing details.
9
10use systemprompt_logging::CliService;
11use systemprompt_models::text::truncate_with_ellipsis;
12
13use super::LogEntryRow;
14
15pub use systemprompt_models::time_format::{format_optional_duration_ms, format_timestamp};
16
17pub fn cost_microdollars_to_dollars(microdollars: i64) -> f64 {
18    microdollars as f64 / 1_000_000.0
19}
20
21pub fn display_log_row(log: &LogEntryRow) {
22    let time_part = if log.timestamp.len() >= 23 {
23        &log.timestamp[11..23]
24    } else {
25        &log.timestamp
26    };
27
28    let trace_short = truncate_with_ellipsis(log.trace_id.as_str(), 8);
29
30    let line = format!(
31        "{} {} [{}] {}  [{}]",
32        time_part, log.level, log.module, log.message, trace_short
33    );
34
35    match log.level.as_str() {
36        "ERROR" => CliService::error(&line),
37        "WARN" => CliService::warning(&line),
38        _ => CliService::info(&line),
39    }
40}