Rust tracing_subscriber guide
Jeff posted on (updated on )
This article will show how to use Rust's tracing_subscriber
crate for certain use cases.
What is logging
Example Rust program:
#[instrument]
pub extern "C" fn divide(a: c_int, b: c_int) -> c_int {
tracing::trace!("dividing {a} by {b}");
if b == 0 {
tracing::error!("Cannot divide by zero");
tracing::debug!("a: {a}, b: {b}");
return 0;
}
let result = a / b;
tracing::info!("{a}/{b}={result}");
result
}
Logging generated by this program:
2024-02-28T22:19:07.883953Z TRACE divide{a=10 b=2}: tracing_rust: dividing 10 by 2
2024-02-28T22:19:07.884076Z INFO divide{a=10 b=2}: tracing_rust: 10/2=5
2024-02-28T22:19:07.884318Z TRACE divide{a=10 b=1}: tracing_rust: dividing 10 by 1
2024-02-28T22:19:07.884369Z INFO divide{a=10 b=1}: tracing_rust: 10/1=10
2024-02-28T22:19:07.884467Z TRACE divide{a=10 b=0}: tracing_rust: dividing 10 by 0
2024-02-28T22:19:07.884525Z ERROR divide{a=10 b=0}: tracing_rust: Cannot divide by zero
2024-02-28T22:19:07.884582Z DEBUG divide{a=10 b=0}: tracing_rust: a: 10, b: 0
What is tracing_subscriber
https://docs.rs/tracing-subscriber/latest/tracing_subscriber/
When it comes to generating/collecting logging from your Rust program, you want to use these 2 crates:
- tracing
- Use this crate to generate logging (like ERROR, INFO, DEBUG loggings)
- tracing-subscriber
- Use this crate to collect and print out the generated logging from
tracing
The usage of tracing
is pretty straightforward, so this article focus on configuring and using tracing-subscriber
TODO
As of writing, the prerelease version tracing GitHub repo has some significant changes over the latest release. I don’t want to waste time on current version only for it to be changed soon