e8576a0196
journald на Linux, stderr-fallback иначе (cfg-gate). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
41 lines
1.5 KiB
Rust
41 lines
1.5 KiB
Rust
//! Инициализация логирования — A10: `tracing` → journald (Linux) / stderr-fallback (dev на macOS / нет демона).
|
|
//! Политика volatile / rate-limit — на стороне journald (настраивается в Плане 5).
|
|
|
|
use tracing_subscriber::prelude::*;
|
|
use tracing_subscriber::EnvFilter;
|
|
|
|
/// Идемпотентно (повторный вызов проглатывается `try_init`). Уровень — из `RUST_LOG`, дефолт `info`.
|
|
pub fn init_tracing(service: &str) {
|
|
let filter = EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info"));
|
|
|
|
#[cfg(target_os = "linux")]
|
|
{
|
|
if let Ok(layer) = tracing_journald::layer() {
|
|
let _ = tracing_subscriber::registry()
|
|
.with(filter)
|
|
.with(layer.with_syslog_identifier(service.to_string()))
|
|
.try_init();
|
|
return;
|
|
}
|
|
}
|
|
|
|
// fallback: stderr (non-Linux или journald недоступен)
|
|
let _ = service;
|
|
let _ = tracing_subscriber::registry()
|
|
.with(filter)
|
|
.with(tracing_subscriber::fmt::layer())
|
|
.try_init();
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn init_does_not_panic() {
|
|
// глобальный subscriber ставится один раз; повторный вызов безопасен (try_init)
|
|
init_tracing("shturman-test");
|
|
tracing::info!("smoke");
|
|
}
|
|
}
|