claude-memory/graph/solutions/file-based-logging-for-rust-tui-apps-using-tracing-appender-1768d9.md
2026-02-28 10:53:58 -06:00

1.9 KiB

id type title tags importance confidence created updated relations
1768d9ab-0c21-4ec3-bb04-bd70366f6240 solution File-based logging for Rust TUI apps using tracing-appender
sba-scout
rust
tracing
logging
tui
ratatui
solution
0.6 0.8 2026-02-28T16:53:12.444250+00:00 2026-02-28T16:53:58.777098+00:00
target type direction strength edge_id
bc4abc6e-57c1-4d6c-b414-213c1367be9b RELATED_TO outgoing 0.9 d07fb339-8eb5-40c8-b63c-fcce499f0fc3
target type direction strength edge_id
2cf3058c-85f4-481a-bbe7-17ebb9b9e908 RELATED_TO outgoing 0.7 8b24bed4-38a2-4a70-8b5e-de2782c897e0

File Logging for Rust TUI Apps

Project: sba-scout (Rust) | Commit: bf7c3f8

Problem

Writing to stdout/stderr corrupts ratatui TUI display. Any tracing output needs to go to a file instead.

Solution

Use tracing-appender with rolling::never() for a fixed log file path.

Dependencies

tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
tracing-appender = "0.2"

Setup

use tracing_appender::rolling;
use tracing_subscriber::EnvFilter;

let file_appender = rolling::never("data", "sba-scout.log");
let (non_blocking, _guard) = tracing_appender::non_blocking(file_appender);

tracing_subscriber::fmt()
    .with_env_filter(EnvFilter::from_default_env())
    .with_writer(non_blocking)
    .init();

// _guard must stay alive for the duration of the app!

Usage

  • Set RUST_LOG=debug (or info, error, etc.) for verbose output
  • Log is written to data/sba-scout.log
  • Add tracing::error!() alongside UI notification sends to capture full error context in log

Critical

The WorkerGuard returned by non_blocking() must be kept alive (stored in a variable that lives for the entire app scope). Dropping it early silently stops log output.