rustrade/logging.rs
1//! Opinionated default tracing subscriber for downstream services that
2//! don't already own one.
3//!
4//! Skippable — if the host service has already installed a
5//! `tracing_subscriber`, calling [`init_tracing`] is a no-op (the
6//! `try_init` call returns an error, which we deliberately swallow).
7
8use tracing_subscriber::EnvFilter;
9
10/// Install a default tracing subscriber.
11///
12/// - Reads filter directives from `RUST_LOG`; defaults to `info` for the
13/// rustrade crates and `warn` for everything else when unset.
14/// - Uses the `tracing-subscriber` "fmt" formatter with target + level +
15/// message; no ANSI colours when not on a TTY.
16/// - Returns `true` if this call installed the subscriber, `false` if a
17/// subscriber was already installed (e.g. by the host) — either way,
18/// logging is wired up.
19pub fn init_tracing() -> bool {
20 let env_filter = EnvFilter::try_from_default_env().unwrap_or_else(|_| {
21 EnvFilter::new(
22 "warn,rustrade=info,rustrade_core=info,rustrade_supervisor=info,rustrade_risk=info",
23 )
24 });
25
26 tracing_subscriber::fmt()
27 .with_env_filter(env_filter)
28 .with_target(true)
29 .with_ansi(std::io::IsTerminal::is_terminal(&std::io::stderr()))
30 .try_init()
31 .is_ok()
32}