Pre-step:
add these to workspace dependencies
tracing = "0.1.37"
tracing-forest = "0.1.6"
tracing-subscriber = "0.3.17"
step 1:
add tracing subscriber to dependencies, namely, the dependencies should have these three
tracing.workspace = true
tracing-subscriber = { workspace = true, features = ["std", "env-filter"] }
tracing-forest = { workspace = true, features = ["ansi", "smallvec"] }
step 2:
add these packages to the file you want to run
use tracing_forest::util::LevelFilter;
use tracing_forest::ForestLayer;
use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::util::SubscriberInitExt;
use tracing_subscriber::{EnvFilter, Registry};
step 3:
add these code to before the execution code that will generate logging info
let env_filter = EnvFilter::builder()
.with_default_directive(LevelFilter::INFO.into()) // Default log level: INFO
.from_env_lossy(); // Allow overrides via RUST_LOG
Registry::default()
.with(env_filter)
.with(ForestLayer::default()) // Hierarchical log formatting
.init();
for example: