feat(common): init_tracing -> journald (A10) + re-exports
journald на Linux, stderr-fallback иначе (cfg-gate). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -6,4 +6,7 @@ pub mod clock;
|
||||
pub mod log;
|
||||
pub mod paths;
|
||||
|
||||
// re-export для эргономики добавляется в Task 5, когда все символы определены.
|
||||
pub use atomic::{tmp_path, write_atomic};
|
||||
pub use clock::monotonic_secs;
|
||||
pub use log::init_tracing;
|
||||
pub use paths::Layout;
|
||||
|
||||
@@ -1 +1,40 @@
|
||||
//! Инициализация логирования — A10 (наполняется в Task 5).
|
||||
//! Инициализация логирования — 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");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user