use anyhow::Result;
use std::path::Path;
use tracing_appender::non_blocking::{NonBlocking, WorkerGuard};
use tracing_subscriber::EnvFilter;
use tracing_subscriber::prelude::*;
pub struct AuditGuard {
_worker_guard: WorkerGuard,
}
pub fn init(log_dir: &Path) -> Result<AuditGuard> {
std::fs::create_dir_all(log_dir)?;
let appender = tracing_appender::rolling::never(log_dir, ".eclipse_audit.log");
let (non_blocking, worker_guard): (NonBlocking, WorkerGuard) =
tracing_appender::non_blocking(appender);
let filter = EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info"));
let file_layer = tracing_subscriber::fmt::layer()
.json()
.with_ansi(false)
.with_target(true)
.with_thread_ids(true)
.with_thread_names(true)
.with_writer(non_blocking);
let console_layer = tracing_subscriber::fmt::layer()
.compact()
.with_target(false);
tracing_subscriber::registry()
.with(filter)
.with(file_layer)
.with(console_layer)
.init();
Ok(AuditGuard {
_worker_guard: worker_guard,
})
}