@@ -11,7 +11,8 @@ use color_eyre::Result;
1111use dialoguer::Confirm;
1212use time::OffsetDateTime;
1313use time::format_description::well_known::Rfc3339;
14−use tracing_subscriber::EnvFilter;
14+use tracing_subscriber::filter::LevelFilter;
15+use tracing_subscriber::{EnvFilter, fmt, prelude::*};
1516
1617use vibebox::tui::{AppState, VmInfo};
1718use vibebox::{
@@ -39,11 +40,11 @@ enum Command {
3940}
4041
4142fn main() -> Result<()> {
42− init_tracing();
4343 color_eyre::install()?;
44+ let cwd = env::current_dir().map_err(|err| color_eyre::eyre::eyre!(err.to_string()))?;
45+ init_tracing(&cwd);
4446
4547 let cli = Cli::parse();
46− let cwd = env::current_dir().map_err(|err| color_eyre::eyre::eyre!(err.to_string()))?;
4748 tracing::info!(cwd = %cwd.display(), "starting vibebox cli");
4849 if let Some(command) = cli.command {
4950 return handle_command(command, &cwd, cli.config.as_deref());
@@ -249,13 +250,58 @@ fn format_last_active(value: Option<&str>) -> String {
249250 format!("{} day{} ago", days, if days == 1 { "" } else { "s" })
250251}
251252
252−fn init_tracing() {
253+fn init_tracing(cwd: &Path) {
253254 let filter = EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info"));
254− let ansi = std::io::stderr().is_terminal() && env::var("VIBEBOX_LOG_NO_COLOR").is_err();
255− let _ = tracing_subscriber::fmt()
256− .with_env_filter(filter)
257− .with_target(false)
258− .with_ansi(ansi)
259− .with_writer(std::io::stderr)
260− .try_init();
255+ let stderr_is_tty = std::io::stderr().is_terminal();
256+ let ansi = stderr_is_tty && env::var("VIBEBOX_LOG_NO_COLOR").is_err();
257+ let file = instance::ensure_instance_dir(cwd)
258+ .ok()
259+ .and_then(|instance_dir| {
260+ let log_path = instance_dir.join("cli.log");
261+ std::fs::OpenOptions::new()
262+ .create(true)
263+ .write(true)
264+ .truncate(true)
265+ .open(log_path)
266+ .ok()
267+ });
268+
269+ if stderr_is_tty {
270+ let stderr_layer = fmt::layer()
271+ .with_target(false)
272+ .with_ansi(ansi)
273+ .without_time()
274+ .with_writer(std::io::stderr)
275+ .with_filter(LevelFilter::INFO);
276+ let subscriber = tracing_subscriber::registry()
277+ .with(filter)
278+ .with(stderr_layer);
279+ if let Some(file) = file {
280+ let file_layer = fmt::layer()
281+ .with_target(false)
282+ .with_ansi(false)
283+ .with_writer(file);
284+ let _ = subscriber.with(file_layer).try_init();
285+ } else {
286+ let _ = subscriber.try_init();
287+ }
288+ } else {
289+ let stderr_layer = fmt::layer()
290+ .with_target(false)
291+ .with_ansi(ansi)
292+ .with_writer(std::io::stderr)
293+ .with_filter(LevelFilter::INFO);
294+ let subscriber = tracing_subscriber::registry()
295+ .with(filter)
296+ .with(stderr_layer);
297+ if let Some(file) = file {
298+ let file_layer = fmt::layer()
299+ .with_target(false)
300+ .with_ansi(false)
301+ .with_writer(file);
302+ let _ = subscriber.with(file_layer).try_init();
303+ } else {
304+ let _ = subscriber.try_init();
305+ }
306+ }
261307}