1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//! Structured logging setup for memd
//!
//! Provides JSON or pretty-formatted logging output.
use tracing_subscriber::{fmt, layer::SubscriberExt, util::SubscriberInitExt, EnvFilter};
/// Initialize the logging system
///
/// # Arguments
/// * `format` - Output format: "json" for structured JSON, "pretty" for human-readable
/// * `level` - Log level: "trace", "debug", "info", "warn", "error"
///
/// Logs are written to stderr so stdout remains machine-readable for CLI
/// commands.
pub fn init_logging(format: &str, level: &str) {
let filter = EnvFilter::try_from_default_env().unwrap_or_else(|_| {
// Keep memd logs at requested level while suppressing noisy third-party info logs
// that significantly slow benchmark runs.
EnvFilter::new(format!("{level},tantivy=warn,hnsw_rs=warn,tokenizers=warn"))
});
match format {
"json" => {
tracing_subscriber::registry()
.with(filter)
.with(
fmt::layer()
.json()
.with_writer(std::io::stderr)
.with_target(true)
.with_file(false)
.with_line_number(false),
)
.init();
}
_ => {
tracing_subscriber::registry()
.with(filter)
.with(
fmt::layer()
.pretty()
.with_writer(std::io::stderr)
.with_target(true),
)
.init();
}
}
}
#[cfg(test)]
mod tests {
// Note: We don't test init_logging directly because tracing can only be
// initialized once per process, and the test runner uses multiple threads.
// The logging is tested indirectly through the integration tests.
}