1.9 KiB
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 |
|
0.6 | 0.8 | 2026-02-28T16:53:12.444250+00:00 | 2026-02-28T16:53:58.777098+00:00 |
|
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(orinfo,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.