|
| 1 | +use tracing::{debug, error, info, instrument, span, warn, Level}; |
| 2 | +use tracing_subscriber::{layer::SubscriberExt, registry::Registry}; |
| 3 | +use tracing_tree::HierarchicalLayer; |
| 4 | + |
| 5 | +fn main() { |
| 6 | + let layer = HierarchicalLayer::default() |
| 7 | + .with_writer(std::io::stdout) |
| 8 | + .with_indent_amount(2) |
| 9 | + .with_thread_names(true) |
| 10 | + .with_thread_ids(true) |
| 11 | + .with_verbose_exit(false) |
| 12 | + .with_verbose_entry(false) |
| 13 | + .with_targets(true); |
| 14 | + |
| 15 | + let subscriber = Registry::default().with(layer); |
| 16 | + tracing::subscriber::set_global_default(subscriber).unwrap(); |
| 17 | + |
| 18 | + let app_span = span!(Level::TRACE, "hierarchical-example", version = %0.1); |
| 19 | + let _e = app_span.enter(); |
| 20 | + |
| 21 | + let server_span = span!(Level::TRACE, "server", host = "localhost", port = 8080); |
| 22 | + let _e2 = server_span.enter(); |
| 23 | + info!("starting"); |
| 24 | + std::thread::sleep(std::time::Duration::from_millis(300)); |
| 25 | + info!("listening"); |
| 26 | + let peer1 = span!(Level::TRACE, "conn", peer_addr = "82.9.9.9", port = 42381); |
| 27 | + peer1.in_scope(|| { |
| 28 | + debug!("connected"); |
| 29 | + std::thread::sleep(std::time::Duration::from_millis(300)); |
| 30 | + debug!(length = 2, "message received"); |
| 31 | + }); |
| 32 | + drop(peer1); |
| 33 | + let peer2 = span!(Level::TRACE, "conn", peer_addr = "8.8.8.8", port = 18230); |
| 34 | + peer2.in_scope(|| { |
| 35 | + std::thread::sleep(std::time::Duration::from_millis(300)); |
| 36 | + debug!("connected"); |
| 37 | + }); |
| 38 | + drop(peer2); |
| 39 | + let peer3 = span!( |
| 40 | + Level::TRACE, |
| 41 | + "foomp", |
| 42 | + normal_var = 43, |
| 43 | + "{} <- format string", |
| 44 | + 42 |
| 45 | + ); |
| 46 | + peer3.in_scope(|| { |
| 47 | + error!("hello"); |
| 48 | + }); |
| 49 | + drop(peer3); |
| 50 | + let peer1 = span!(Level::TRACE, "conn", peer_addr = "82.9.9.9", port = 42381); |
| 51 | + peer1.in_scope(|| { |
| 52 | + warn!(algo = "xor", "weak encryption requested"); |
| 53 | + std::thread::sleep(std::time::Duration::from_millis(300)); |
| 54 | + debug!(length = 8, "response sent"); |
| 55 | + debug!("disconnected"); |
| 56 | + }); |
| 57 | + drop(peer1); |
| 58 | + let peer2 = span!(Level::TRACE, "conn", peer_addr = "8.8.8.8", port = 18230); |
| 59 | + peer2.in_scope(|| { |
| 60 | + debug!(length = 5, "message received"); |
| 61 | + std::thread::sleep(std::time::Duration::from_millis(300)); |
| 62 | + debug!(length = 8, "response sent"); |
| 63 | + debug!("disconnected"); |
| 64 | + }); |
| 65 | + drop(peer2); |
| 66 | + warn!("internal error"); |
| 67 | + info!("exit"); |
| 68 | +} |
| 69 | + |
| 70 | +#[instrument] |
| 71 | +fn call_a(name: &str) { |
| 72 | + info!(name, "got a name"); |
| 73 | + call_b(name) |
| 74 | +} |
| 75 | + |
| 76 | +#[instrument] |
| 77 | +fn call_b(name: &str) { |
| 78 | + info!(name, "got a name"); |
| 79 | +} |
0 commit comments