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