rustic_core
rustic_core copied to clipboard
use `tracing` crate for logging
I think it would be nice to use the tracing crate throughout rustic_core, as we could make use of it also regarding the #[instrument] macro. Giving us an easy opportunity to track calls into methods/functions as seen here: https://github.com/tokio-rs/tracing?tab=readme-ov-file#in-libraries
the
#[tracing::instrument]attribute creates and enters a span every time the instrumented function is called. The span is named after the function or method. Parameters passed to the function are recorded as fields.
This will make debugging much easier for downstream users e.g. in rustic, and we will also get much more information and thus insight into (future?) asynchronous code.
The nice thing about the migration: it uses the same log facade, so we can just start annotating methods with tracing::instrument and otherwise replace log:: with tracing::.
Structured logging
- we can log key-value pairs along with the message and easily use json output
// Using tracing
tracing::info!(tree_id = ea56df32, "TreeArchiver bla");
// Using log
log::info!("TreeArchiver bla tree id ea56df32");
Span-based Logging and Context Propagation
- spans can represent a unit of work, spans can nest, any event or log that occurs within a span inherits its context (nice for asynchronous code)
let span = tracing::info_span!("handle_pack", tree_id = ea56df32);
let _enter = span.enter(); // Enters the span, attaching context
tracing::info!("Processing Pack");
Log Filtering
- fine-grained control over what is logged, attachable metadata to events and spans, and configurable filters based on span context, levels, or specific fields
tracing::info!(target: "index_queries", latency_ms = 11, "Querying Index");
// Can be filtered dynamically to only include logs from "index_queries" above a certain latency threshold
Instrumentation
- spans and events can be useful for performance monitoring and profiling tasks
Extensibility with Subscribers
- Custom subscribers control how spans and events are recorded. Subscribers can filter, format, and export logs in a custom way. That can be useful for integrating with external systems.