Skip to main content

combined/
combined.rs

1//! Combined stdout + journald example. The pretty span-chain output goes
2//! to stdout for human reading; the journal receives structured events
3//! via `tracing-journald` so they are queryable with `journalctl`.
4//!
5//! Run with: `cargo run --example combined --features journald`
6
7use tracing::{error, info, instrument, warn};
8use tracing_subscriber::prelude::*;
9use tracing_systemd::SystemdLayer;
10
11fn main() {
12    // Stdout layer: pretty for humans.
13    let stdout_layer = SystemdLayer::stdout()
14        .with_target(true)
15        .with_level_prefix(false);
16
17    // Journald layer: optional. None if we're not running under systemd.
18    let journald_layer = tracing_systemd::journald::layer_with_identifier("combined-example").ok();
19
20    tracing_subscriber::registry()
21        .with(stdout_layer)
22        .with(journald_layer)
23        .init();
24
25    work(42);
26}
27
28#[instrument]
29fn work(seed: u64) {
30    info!("starting work");
31    if seed % 2 == 0 {
32        warn!(seed, "even seed — heuristic only");
33    } else {
34        error!(seed, "odd seed — would have failed");
35    }
36}