use anyhow::Error; use tokio::task::JoinSet; use tracing::{debug, info, instrument, span, Instrument as _, Level}; use tracing_glog::{Glog, GlogFields, LocalTime}; #[instrument(skip_all)] async fn no_fields() { info!("Printing from no_fields"); sub_no_fields().await; } #[instrument(skip_all)] async fn sub_no_fields() { info!("Printing from sub_no_fields"); } #[instrument] async fn parent_task(subtasks: usize, reason: &str) -> Result<(), Error> { info!("spawning subtasks in {reason}..."); let mut set = JoinSet::new(); for number in 1..=subtasks { let span = span!(Level::INFO, "subtask", %number); debug!(message = "creating subtask;", number); set.spawn(subtask(number).instrument(span)); } // the returnable error would be if one of the subtasks panicked. while let Some(task) = set.join_next().await { let task = task?; debug!(%task, "task completed"); } Ok(()) } async fn subtask(number: usize) -> usize { info!(%number, "polling subtask"); number } #[tokio::main] async fn main() -> Result<(), Error> { tracing_subscriber::fmt() .with_ansi(true) .event_format( Glog::default() .with_target(false) .with_thread_names(false) .with_timer(LocalTime::default()) .with_span_names(false), ) .fmt_fields(GlogFields::default().compact()) .init(); parent_task(10, "testing").await?; no_fields().await; Ok(()) }