geph5_client/
logging.rs

1use arc_writer::ArcWriter;
2use once_cell::sync::Lazy;
3use tracing_subscriber::{fmt, layer::SubscriberExt, util::SubscriberInitExt, EnvFilter};
4
5/// In-memory buffer for JSON formatted logs
6static JSON_LOGS: Lazy<ArcWriter<Vec<u8>>> = Lazy::new(|| ArcWriter::new(Vec::new()));
7
8/// Initialize the tracing subscribers for logging
9pub fn init_logging() -> anyhow::Result<()> {
10    // Create a JSON writer that writes to our in-memory buffer
11    let json_writer = JSON_LOGS.clone();
12
13    tracing_subscriber::registry()
14        // Standard logs to stderr (for console display)
15        .with(fmt::layer().compact().with_writer(std::io::stderr))
16        // Text logs to the LOGS global buffer
17        .with(fmt::layer().json().with_writer(move || json_writer.clone()))
18        // Set filtering based on environment or defaults
19        .with(
20            EnvFilter::builder()
21                .with_default_directive("geph=debug".parse()?)
22                .from_env_lossy(),
23        )
24        .init();
25
26    Ok(())
27}
28
29/// Get the current JSON logs as a String
30pub fn get_json_logs() -> String {
31    let logs = JSON_LOGS.lock();
32
33    // Convert the byte buffer to a string
34    let log_string = String::from_utf8_lossy(&logs);
35
36    log_string.to_string()
37}