PluralKit/lib/libpk/src/lib.rs
2023-02-15 19:27:36 -05:00

27 lines
1,007 B
Rust

use gethostname::gethostname;
use tracing_subscriber::{prelude::__tracing_subscriber_SubscriberExt, EnvFilter, Registry};
mod _config;
pub use crate::_config::CONFIG as config;
pub fn init_logging(component: &str) -> anyhow::Result<()> {
let subscriber = Registry::default()
.with(EnvFilter::from_default_env())
.with(tracing_subscriber::fmt::layer());
if let Some(gelf_url) = &config.gelf_log_url {
let gelf_logger = tracing_gelf::Logger::builder()
.additional_field("component", component)
.additional_field("hostname", gethostname().to_str());
let mut conn_handle = gelf_logger
.init_udp_with_subscriber(gelf_url, subscriber)
.unwrap();
tokio::spawn(async move { conn_handle.connect().await });
} else {
// gelf_logger internally sets the global subscriber
tracing::subscriber::set_global_default(subscriber)
.expect("unable to set global subscriber");
}
Ok(())
}