use osproxy_observe::DiagnosticSink;
use serde_json::Value;
pub trait RequestLog: Send + Sync {
fn enabled(&self) -> bool {
true
}
fn emit(&self, record: &Value);
}
#[derive(Clone, Copy, Debug, Default)]
pub struct NoLog;
impl RequestLog for NoLog {
fn enabled(&self) -> bool {
false
}
fn emit(&self, _record: &Value) {}
}
#[derive(Clone, Copy, Debug, Default)]
pub struct StdoutJsonLog;
impl RequestLog for StdoutJsonLog {
fn emit(&self, record: &Value) {
println!("{record}");
}
}
#[derive(Clone, Copy, Debug, Default)]
pub struct StdoutDiagnosticSink;
impl DiagnosticSink for StdoutDiagnosticSink {
fn emit(&self, doc: Value) {
println!(
"{}",
serde_json::json!({ "kind": "diagnostic_capture", "capture": doc })
);
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_default_logger_is_disabled() {
assert!(!NoLog.enabled());
NoLog.emit(&serde_json::json!({})); }
#[test]
fn the_stdout_diagnostic_sink_is_enabled_and_does_not_panic() {
assert!(StdoutDiagnosticSink.enabled());
StdoutDiagnosticSink.emit(serde_json::json!({"trace_id": "abc"}));
}
}