]> Untitled Git - lemmy.git/blob - src/lib.rs
3dcc41e58605043f7d7630406baf3ad045bc7511
[lemmy.git] / src / lib.rs
1 #![recursion_limit = "512"]
2 pub mod api_routes;
3 pub mod code_migrations;
4 pub mod root_span_builder;
5 pub mod scheduled_tasks;
6 #[cfg(feature = "console")]
7 pub mod telemetry;
8
9 use lemmy_utils::error::LemmyError;
10 use tracing::subscriber::set_global_default;
11 use tracing_error::ErrorLayer;
12 use tracing_log::LogTracer;
13 use tracing_subscriber::{filter::Targets, layer::SubscriberExt, Layer, Registry};
14 use url::Url;
15
16 pub fn init_logging(opentelemetry_url: &Option<Url>) -> Result<(), LemmyError> {
17   LogTracer::init()?;
18
19   let log_description = std::env::var("RUST_LOG").unwrap_or_else(|_| "info".into());
20
21   let targets = log_description
22     .trim()
23     .trim_matches('"')
24     .parse::<Targets>()?;
25
26   let format_layer = tracing_subscriber::fmt::layer().with_filter(targets.clone());
27
28   let subscriber = Registry::default()
29     .with(format_layer)
30     .with(ErrorLayer::default());
31
32   if let Some(_url) = opentelemetry_url {
33     #[cfg(feature = "console")]
34     telemetry::init_tracing(_url.as_ref(), subscriber, targets)?;
35     #[cfg(not(feature = "console"))]
36     tracing::error!("Feature `console` must be enabled for opentelemetry tracing");
37   } else {
38     set_global_default(subscriber)?;
39   }
40
41   Ok(())
42 }