lemmy/src/lib.rs

43 lines
1.2 KiB
Rust
Raw Normal View History

pub mod api_routes_http;
pub mod api_routes_websocket;
pub mod code_migrations;
pub mod root_span_builder;
pub mod scheduled_tasks;
#[cfg(feature = "console")]
2022-05-10 08:06:32 -04:00
pub mod telemetry;
use lemmy_utils::error::LemmyError;
2021-11-23 07:16:47 -05: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 07:16:47 -05:00
pub fn init_logging(opentelemetry_url: &Option<Url>) -> Result<(), LemmyError> {
2021-11-23 07:16:47 -05: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 07:16:47 -05:00
let subscriber = Registry::default()
.with(format_layer)
.with(ErrorLayer::default());
2022-05-10 08:06:32 -04:00
if let Some(_url) = opentelemetry_url {
#[cfg(feature = "console")]
telemetry::init_tracing(_url.as_ref(), subscriber, targets)?;
2022-05-10 08:06:32 -04:00
#[cfg(not(feature = "console"))]
tracing::error!("Feature `console` must be enabled for opentelemetry tracing");
} else {
set_global_default(subscriber)?;
}
2021-11-23 07:16:47 -05:00
Ok(())
}