2021-03-22 12:46:10 +01:00
|
|
|
use tracing::{subscriber::set_global_default, Subscriber};
|
|
|
|
use tracing_bunyan_formatter::{BunyanFormattingLayer, JsonStorageLayer};
|
|
|
|
use tracing_log::LogTracer;
|
|
|
|
use tracing_subscriber::{layer::SubscriberExt, EnvFilter, Registry};
|
|
|
|
|
|
|
|
pub fn get_subscriber(name: &str, env_filter: &str) -> impl Subscriber + Send + Sync {
|
|
|
|
let env_filter =
|
|
|
|
EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new(env_filter));
|
|
|
|
|
|
|
|
let formatting_layer = BunyanFormattingLayer::new(name.to_string(), std::io::stdout);
|
|
|
|
|
|
|
|
Registry::default()
|
|
|
|
.with(env_filter)
|
|
|
|
.with(JsonStorageLayer)
|
|
|
|
.with(formatting_layer)
|
|
|
|
}
|
|
|
|
|
2023-08-27 14:34:14 +02:00
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// This panics if the `LogTracer` cannot be initialized or `subscriber` cannot be set as global
|
|
|
|
/// default.
|
2021-03-22 12:46:10 +01:00
|
|
|
pub fn init_subscriber(subscriber: impl Subscriber + Send + Sync) {
|
|
|
|
LogTracer::init().expect("Failed to set logger");
|
|
|
|
set_global_default(subscriber).expect("Failed to set tracing subscriber");
|
|
|
|
}
|