Skip to main content

json/
json.rs

1//! JSON output example. One JSON object per line.
2//!
3//! Run with: `cargo run --example json --features json`
4//!
5//! Pipe through `jq` to inspect:
6//! `cargo run --example json --features json 2>&1 | jq`
7
8use tracing::{error, info, instrument, warn};
9use tracing_subscriber::prelude::*;
10use tracing_systemd::SystemdLayer;
11
12fn main() {
13    tracing_subscriber::registry()
14        .with(SystemdLayer::json().with_thread_ids(true))
15        .init();
16
17    handle_request("GET", "/api/users");
18    handle_request("POST", "/api/orders");
19}
20
21#[instrument]
22fn handle_request(method: &str, path: &str) {
23    info!(method, path, "received");
24    work(42);
25    if path.contains("orders") {
26        warn!(retries = 2u64, "transient db error, retrying");
27    }
28    error!(error = "io: connection reset", "request failed");
29}
30
31#[instrument]
32fn work(items: u64) {
33    info!(processed = items, "done");
34}