fabro/lib/crates/arc-cli/src/logging.rs
Bryan Helmkamp 54ecae9586 Write per-run tracing logs to cli.log inside each run directory
Adds a switchable tracing layer that writes to ~/.arc/logs/{run-dir}/cli.log,
so all diagnostic info for a run is co-located with its other artifacts instead
of buried in the shared daily log file.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-10 09:30:00 -04:00

44 lines
1.3 KiB
Rust

use anyhow::{Context, Result};
use tracing_subscriber::{fmt, layer::SubscriberExt, util::SubscriberInitExt, EnvFilter};
pub fn init_tracing(debug: bool, config_log_level: Option<&str>, log_prefix: &str) -> Result<()> {
let default_level = if debug {
"debug"
} else {
config_log_level.unwrap_or("info")
};
let filter =
EnvFilter::try_from_env("ARC_LOG").unwrap_or_else(|_| EnvFilter::new(default_level));
let log_dir = dirs::home_dir()
.map(|h| h.join(".arc").join("logs"))
.unwrap_or_else(|| ".arc/logs".into());
std::fs::create_dir_all(&log_dir)
.with_context(|| format!("Failed to create log directory: {}", log_dir.display()))?;
let filename = chrono::Local::now()
.format(&format!("{log_prefix}-%Y-%m-%d.log"))
.to_string();
let file_appender = tracing_appender::rolling::never(&log_dir, &filename);
let run_log_writer = arc_util::run_log::init();
tracing_subscriber::registry()
.with(filter)
.with(
fmt::layer()
.with_writer(file_appender)
.with_target(true)
.with_ansi(false),
)
.with(
fmt::layer()
.with_writer(run_log_writer)
.with_target(true)
.with_ansi(false),
)
.init();
Ok(())
}