fedimint_logging/
lib.rs
1#![deny(clippy::pedantic)]
2#![allow(clippy::missing_errors_doc)]
3#![allow(clippy::missing_panics_doc)]
4
5use std::fs::File;
18use std::{env, io};
19
20use tracing_subscriber::layer::SubscriberExt;
21use tracing_subscriber::util::SubscriberInitExt;
22use tracing_subscriber::{EnvFilter, Layer};
23
24pub const LOG_CONSENSUS: &str = "fm::consensus";
25pub const LOG_CORE: &str = "fm::core";
26pub const LOG_SERVER: &str = "fm::server";
27pub const LOG_DB: &str = "fm::db";
28pub const LOG_DEVIMINT: &str = "fm::devimint";
29pub const LOG_NET: &str = "fm::net";
30pub const LOG_NET_IROH: &str = "fm::net::iroh";
31pub const LOG_NET_WS: &str = "fm::net::ws";
32pub const LOG_NET_API: &str = "fm::net::api";
33pub const LOG_NET_PEER_DKG: &str = "fm::net::peer::dkg";
34pub const LOG_NET_PEER: &str = "fm::net::peer";
35pub const LOG_NET_AUTH: &str = "fm::net::auth";
36pub const LOG_TASK: &str = "fm::task";
37pub const LOG_RUNTIME: &str = "fm::runtime";
38pub const LOG_TEST: &str = "fm::test";
39pub const LOG_TIMING: &str = "fm::timing";
40pub const LOG_CLIENT: &str = "fm::client";
41pub const LOG_CLIENT_DB: &str = "fm::client::db";
42pub const LOG_CLIENT_EVENT_LOG: &str = "fm::client::event-log";
43pub const LOG_MODULE_MINT: &str = "fm::module::mint";
44pub const LOG_MODULE_META: &str = "fm::module::meta";
45pub const LOG_MODULE_WALLET: &str = "fm::module::wallet";
46pub const LOG_MODULE_LN: &str = "fm::module::ln";
47pub const LOG_MODULE_LNV2: &str = "fm::module::lnv2";
48pub const LOG_CLIENT_REACTOR: &str = "fm::client::reactor";
49pub const LOG_CLIENT_NET_API: &str = "fm::client::net::api";
50pub const LOG_CLIENT_BACKUP: &str = "fm::client::backup";
51pub const LOG_CLIENT_RECOVERY: &str = "fm::client::recovery";
52pub const LOG_CLIENT_RECOVERY_MINT: &str = "fm::client::recovery::mint";
53pub const LOG_CLIENT_MODULE_MINT: &str = "fm::client::module::mint";
54pub const LOG_CLIENT_MODULE_META: &str = "fm::client::module::meta";
55pub const LOG_CLIENT_MODULE_LN: &str = "fm::client::module::ln";
56pub const LOG_CLIENT_MODULE_LNV2: &str = "fm::client::module::lnv2";
57pub const LOG_CLIENT_MODULE_WALLET: &str = "fm::client::module::wallet";
58pub const LOG_CLIENT_MODULE_GW: &str = "fm::client::module::gw";
59pub const LOG_CLIENT_MODULE_GWV2: &str = "fm::client::module::gwv2";
60pub const LOG_GATEWAY: &str = "fm::gw";
61pub const LOG_LIGHTNING: &str = "fm::gw::lightning";
62pub const LOG_BITCOIND_ESPLORA: &str = "fm::bitcoind::esplora";
63pub const LOG_BITCOIND_CORE: &str = "fm::bitcoind::bitcoincore";
64pub const LOG_BITCOIND: &str = "fm::bitcoind";
65pub const LOG_BITCOIN: &str = "fm::bitcoin";
66
67#[derive(Default)]
69pub struct TracingSetup {
70 base_level: Option<String>,
71 extra_directives: Option<String>,
72 #[cfg(feature = "telemetry")]
73 tokio_console_bind: Option<std::net::SocketAddr>,
74 #[cfg(feature = "telemetry")]
75 with_jaeger: bool,
76 with_file: Option<File>,
77}
78
79impl TracingSetup {
80 #[cfg(feature = "telemetry")]
82 pub fn tokio_console_bind(&mut self, address: Option<std::net::SocketAddr>) -> &mut Self {
83 self.tokio_console_bind = address;
84 self
85 }
86
87 #[cfg(feature = "telemetry")]
89 pub fn with_jaeger(&mut self, enabled: bool) -> &mut Self {
90 self.with_jaeger = enabled;
91 self
92 }
93
94 pub fn with_file(&mut self, file: Option<File>) -> &mut Self {
95 self.with_file = file;
96 self
97 }
98
99 pub fn with_base_level(&mut self, level: impl Into<String>) -> &mut Self {
103 self.base_level = Some(level.into());
104 self
105 }
106
107 pub fn with_directive(&mut self, directive: &str) -> &mut Self {
109 if let Some(old) = self.extra_directives.as_mut() {
110 *old = format!("{old},{directive}");
111 } else {
112 self.extra_directives = Some(directive.to_owned());
113 }
114 self
115 }
116
117 pub fn init(&mut self) -> anyhow::Result<()> {
119 use tracing_subscriber::fmt::writer::{BoxMakeWriter, Tee};
120
121 let var = env::var(tracing_subscriber::EnvFilter::DEFAULT_ENV).unwrap_or_default();
122 let filter_layer = EnvFilter::builder().parse(format!(
123 "{},{},{},{},{},{},{},{}",
127 self.base_level.as_deref().unwrap_or("info"),
128 "jsonrpsee_core::client::async_client=off",
129 "hyper=off",
130 "h2=off",
131 "jsonrpsee_server=warn,jsonrpsee_server::transport=off",
132 "AlephBFT-=error",
133 var,
134 self.extra_directives.as_deref().unwrap_or(""),
135 ))?;
136
137 let fmt_writer = match self.with_file.take() {
138 Some(file) => BoxMakeWriter::new(Tee::new(io::stderr, file)),
139 _ => BoxMakeWriter::new(io::stderr),
140 };
141
142 let fmt_layer = tracing_subscriber::fmt::layer()
143 .with_thread_names(false) .with_writer(fmt_writer)
145 .with_filter(filter_layer);
146
147 let console_opt = || -> Option<Box<dyn Layer<_> + Send + Sync + 'static>> {
148 #[cfg(feature = "telemetry")]
149 if let Some(l) = self.tokio_console_bind {
150 let tracer = console_subscriber::ConsoleLayer::builder()
151 .retention(std::time::Duration::from_secs(60))
152 .server_addr(l)
153 .spawn()
154 .with_filter(EnvFilter::new("tokio=trace,runtime=trace"));
156 return Some(tracer.boxed());
157 }
158 None
159 };
160
161 let telemetry_layer_opt = || -> Option<Box<dyn Layer<_> + Send + Sync + 'static>> {
162 #[cfg(feature = "telemetry")]
163 if self.with_jaeger {
164 #[allow(deprecated)]
166 let tracer = opentelemetry_jaeger::new_agent_pipeline()
167 .with_service_name("fedimint")
168 .install_simple()
169 .unwrap();
170
171 return Some(tracing_opentelemetry::layer().with_tracer(tracer).boxed());
172 }
173 None
174 };
175
176 tracing_subscriber::registry()
177 .with(fmt_layer)
178 .with(console_opt())
179 .with(telemetry_layer_opt())
180 .try_init()?;
181 Ok(())
182 }
183}
184
185pub fn shutdown() {
186 #[cfg(feature = "telemetry")]
187 opentelemetry::global::shutdown_tracer_provider();
188}