gbserver/gb-monitoring/src/logging.rs

41 lines
1.1 KiB
Rust
Raw Normal View History

2024-12-23 00:20:59 -03:00
use tracing::subscriber::set_global_default;
2024-12-22 20:56:52 -03:00
use tracing_subscriber::{
2024-12-23 00:20:59 -03:00
fmt::{format::FmtSpan, time},
EnvFilter,
2024-12-22 20:56:52 -03:00
layer::SubscriberExt,
2024-12-23 00:20:59 -03:00
Registry,
2024-12-22 20:56:52 -03:00
};
2024-12-26 10:09:39 -03:00
pub fn init_logging(_service_name: &str) -> Result<(), Box<dyn std::error::Error>> {
2024-12-22 20:56:52 -03:00
let env_filter = EnvFilter::try_from_default_env()
.unwrap_or_else(|_| EnvFilter::new("info"));
let formatting_layer = tracing_subscriber::fmt::layer()
2024-12-23 00:20:59 -03:00
.with_timer(time::time())
2024-12-22 20:56:52 -03:00
.with_target(true)
2024-12-23 00:20:59 -03:00
.with_thread_ids(true)
.with_span_events(FmtSpan::CLOSE)
2024-12-22 20:56:52 -03:00
.with_file(true)
2024-12-23 00:20:59 -03:00
.with_line_number(true);
2024-12-22 20:56:52 -03:00
let subscriber = Registry::default()
.with(env_filter)
.with(formatting_layer);
2024-12-25 16:25:09 -03:00
set_global_default(subscriber)?; // Use ? instead of expect
Ok(())
2024-12-22 20:56:52 -03:00
}
#[cfg(test)]
mod tests {
2024-12-26 10:09:39 -03:00
2024-12-22 20:56:52 -03:00
use tracing::info;
#[test]
fn test_logging_initialization() {
2024-12-26 10:09:39 -03:00
// TODO: init_logging("gb").Result; // Just call the function
2024-12-22 20:56:52 -03:00
info!("Test log message");
2024-12-25 16:25:09 -03:00
// Add assertions to verify the log was actually written if needed
2024-12-22 20:56:52 -03:00
}
}