manta_shared/common/
log_ops.rs

1//! Tracing-subscriber initialisation shared by both binaries.
2//!
3//! Both `manta-cli` and `manta-server` call [`configure`] exactly once
4//! at startup, after the config file has supplied a `log` directive.
5//! Centralising the setup here keeps target filtering, the `log →
6//! tracing` bridge, and the timestamp toggle consistent across the two
7//! binaries — useful when grepping logs from a CLI invocation that
8//! triggered a server-side action.
9
10use tracing_subscriber::EnvFilter;
11
12/// Configure the global tracing subscriber and bridge `log::` calls into it.
13///
14/// `log_level` is an `EnvFilter` directive string, e.g. `"info"`, `"debug"`,
15/// or `"manta=debug,hyper=warn"`. Falls back to `"error"` on parse failure.
16///
17/// `with_timestamps` controls whether each emitted line is prefixed with
18/// the local time. The long-running server enables this so operators can
19/// correlate events across requests; the interactive CLI disables it to
20/// keep terminal output uncluttered.
21///
22/// Call this exactly once per process; subsequent calls are no-ops
23/// (the subscriber is global and `init()` is idempotent-ish — it
24/// panics on a second install, so guard with `OnceCell` if you need
25/// to re-configure).
26///
27/// # Examples
28///
29/// Typical CLI startup — no timestamps, simple level:
30///
31/// ```no_run
32/// use manta_shared::common::log_ops;
33///
34/// log_ops::configure("info", false);
35/// tracing::info!("manta-cli starting");
36/// ```
37///
38/// Server startup with per-target filtering:
39///
40/// ```no_run
41/// use manta_shared::common::log_ops;
42///
43/// log_ops::configure("manta=debug,hyper=warn,tower_http=info", true);
44/// ```
45pub fn configure(log_level: &str, with_timestamps: bool) {
46  let filter =
47    EnvFilter::try_new(log_level).unwrap_or_else(|_| EnvFilter::new("error"));
48
49  let builder = tracing_subscriber::fmt()
50    .with_env_filter(filter)
51    .with_target(false);
52
53  if with_timestamps {
54    builder.init();
55  } else {
56    builder.without_time().init();
57  }
58}