HiveBrain v1.2.0
Get Started
← Back to all entries
snippetrustModerate

How to use the tracing library?

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
howusethelibrarytracing

Problem

I have a very simple code example here which just wants to get the first item of a list I pass in. I have been trying to instrument the code with tracing to help debug the application but frankly the documentation is too sophisticated for me to completely understand.
use std::fmt::Debug;
use tracing::{span, Level, event};

fn main() {
pub fn calculate(data_set: [T; N]) -> (i32, i32) {

// Tracing BoilerPlate
event!(Level::INFO, "something happened");
let span = span!(Level::INFO, "my_span");
let _guard = span.enter();

// Key Code
let _var = data_set.get(0);
println!("Output_1: {:?}", data_set.get(0));

event!(Level::DEBUG, "something happened inside my_span");

// Ignore
return (0, 0)
}

let data = [1509, 1857, 1736, 1815, 1576];
let _result = calculate(data);
}


Specifically here, I don't understand where I can view the event! logs. They don't seem to print out to any window or file or anything.

Can someone direct me as to where to find these debug logs or provide me with a simplified explanation of how to use the tracing crate?

Solution

Here is a basic implementation of tracing which outputs information to a log file (debug.log in repository root) and/or stdout (with more code) using the partner crate called tracing-subscriber

To find these tangible examples of how to bring tracing and tracing-subscriber together, you have to go very deep into the docs: go to the tracing-subscriber crate, then go to the Layer trait, then go to Module level documentation. Here is an alternative link.

frankly the documentation is too sophisticated for me to completely understand.

I'd argue there is significant work to be done for the documentation of these two crates. Objectively, it does not make it easy for beginners to understand what is happening. The docs describe the crate in highly sophisticated language but fail to properly teach newcomers the basics. More complete resources or quick start guides need to be created on this topic.

More simpler debugging tools are available which properly teach how you how to use it's crate. Flexi-logger provides a code examples documentation page which explains proficiently how to use the crate. Flexi-logger works in tandem with the Log crate to output log files as well as to stdout and stderr.

Here is the Tracing Implementation
use tracing_subscriber::{filter, prelude::*};
use std::{fs::File, sync::Arc};

// A layer that logs events to stdout using the human-readable "pretty"
// format.
fn main() {
let stdout_log = tracing_subscriber::fmt::layer()
.pretty();

// A layer that logs events to a file.
let file = File::create("debug.log");
let file = match file {Ok(file) => file,Err(error) => panic!("Error: {:?}",error),};
let debug_log = tracing_subscriber::fmt::layer()
.with_writer(Arc::new(file));

// A layer that collects metrics using specific events.
let metrics_layer = / ... / filter::LevelFilter::INFO;

tracing_subscriber::registry()
.with(
stdout_log
// Add an
INFO filter to the stdout logging layer
.with_filter(filter::LevelFilter::INFO)
// Combine the filtered
stdout_log layer with the
//
debug_log layer, producing a new Layered layer.
.and_then(debug_log)
// Add a filter to both layers that rejects spans and
// events whose targets start with
metrics.
.with_filter(filter::filter_fn(|metadata| {
!metadata.target().starts_with("metrics")
}))
)
.with(
// Add a filter to the metrics label that only enables
// events whose targets start with
metrics.
metrics_layer.with_filter(filter::filter_fn(|metadata| {
metadata.target().starts_with("metrics")
}))
)
.init();

// This event will only be recorded by the metrics layer.
tracing::info!(target: "metrics::cool_stuff_count", value = 42);

// This event will only be seen by the debug log file layer:
tracing::debug!("this is a message, and part of a system of messages");

// This event will be seen by both the stdout log layer and
// the debug log file layer, but not by the metrics layer.
tracing::warn!("the message is a warning about danger!");

}

Context

Stack Overflow Q#70013172, score: 43

Revisions (0)

No revisions yet.