lemmy/src/lib.rs

42 lines
1.1 KiB
Rust
Raw Normal View History

2021-03-25 19:30:15 +00:00
pub mod api_routes;
pub mod code_migrations;
pub mod root_span_builder;
pub mod scheduled_tasks;
#[cfg(feature = "console")]
2022-05-10 12:06:32 +00:00
pub mod telemetry;
use lemmy_utils::error::LemmyError;
2021-11-23 12:16:47 +00:00
use tracing::subscriber::set_global_default;
use tracing_error::ErrorLayer;
use tracing_log::LogTracer;
use tracing_subscriber::{filter::Targets, layer::SubscriberExt, Layer, Registry};
use url::Url;
2021-11-23 12:16:47 +00:00
pub fn init_logging(opentelemetry_url: &Option<Url>) -> Result<(), LemmyError> {
2021-11-23 12:16:47 +00:00
LogTracer::init()?;
let log_description = std::env::var("RUST_LOG").unwrap_or_else(|_| "info".into());
let targets = log_description
.trim()
.trim_matches('"')
.parse::<Targets>()?;
let format_layer = tracing_subscriber::fmt::layer().with_filter(targets.clone());
2021-11-23 12:16:47 +00:00
let subscriber = Registry::default()
.with(format_layer)
.with(ErrorLayer::default());
2022-05-10 12:06:32 +00:00
if let Some(_url) = opentelemetry_url {
#[cfg(feature = "console")]
telemetry::init_tracing(_url.as_ref(), subscriber, targets)?;
2022-05-10 12:06:32 +00:00
#[cfg(not(feature = "console"))]
tracing::error!("Feature `console` must be enabled for opentelemetry tracing");
} else {
set_global_default(subscriber)?;
}
2021-11-23 12:16:47 +00:00
Ok(())
}