1use arc_writer::ArcWriter;
2use once_cell::sync::Lazy;
3use std::sync::{Arc, Mutex};
4use tracing_subscriber::{fmt, layer::SubscriberExt, util::SubscriberInitExt, EnvFilter};
5
6static JSON_LOGS: Lazy<ArcWriter<Vec<u8>>> = Lazy::new(|| ArcWriter::new(Vec::new()));
8
9pub fn init_logging() -> anyhow::Result<()> {
11 let json_writer = JSON_LOGS.clone();
13
14 tracing_subscriber::registry()
15 .with(fmt::layer().compact().with_writer(std::io::stderr))
17 .with(fmt::layer().json().with_writer(move || json_writer.clone()))
19 .with(
21 EnvFilter::builder()
22 .with_default_directive("geph=debug".parse()?)
23 .from_env_lossy(),
24 )
25 .init();
26
27 Ok(())
28}
29
30pub fn get_json_logs() -> String {
32 let logs = JSON_LOGS.lock();
33
34 let log_string = String::from_utf8_lossy(&logs);
36
37 log_string.to_string()
38}