diff --git a/Cargo.lock b/Cargo.lock index 3e78e1e..4ef7d51 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -662,6 +662,7 @@ dependencies = [ "tokio", "toml", "tracing", + "tracing-journald", "tracing-subscriber", "url", ] @@ -1540,6 +1541,17 @@ dependencies = [ "valuable", ] +[[package]] +name = "tracing-journald" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba316a74e8fc3c3896a850dba2375928a9fa171b085ecddfc7c054d39970f3fd" +dependencies = [ + "libc", + "tracing-core", + "tracing-subscriber", +] + [[package]] name = "tracing-log" version = "0.2.0" diff --git a/Cargo.toml b/Cargo.toml index 8f6e229..b0d3da9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,5 +19,6 @@ thiserror = "1.0.63" tokio = { version = "1.39.1", default-features = false, features = ["rt", "macros", "rt-multi-thread", "signal", "process"] } toml = "0.8.15" tracing = "0.1.40" +tracing-journald = "0.3.0" tracing-subscriber = { version = "0.3.18", features = ["env-filter"] } url = { version = "2.5.2", features = ["serde"] } diff --git a/src/main.rs b/src/main.rs index d0e97ed..1583ef5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,16 +11,34 @@ use std::time::Duration; use tokio::signal::unix::SignalKind; use tokio::sync::Notify; use tracing::{debug, error, info}; +use tracing_subscriber::prelude::*; use config::Configuration; #[tokio::main] async fn main() { - tracing_subscriber::fmt() - .without_time() - .with_env_filter(tracing_subscriber::EnvFilter::from_default_env()) - .with_writer(std::io::stderr) - .init(); + match tracing_journald::layer() { + Ok(l) => { + let subscriber = tracing_subscriber::Registry::default() + .with(l) + .with(tracing_subscriber::EnvFilter::from_default_env()); + if let Err(e) = tracing::subscriber::set_global_default(subscriber) + { + eprintln!("Failed to set global tracing subscriber: {}", e); + } + } + Err(e) => { + if e.kind() != std::io::ErrorKind::NotFound { + eprintln!("Unable to write to journald socket: {}", e); + } + tracing_subscriber::fmt() + .with_env_filter( + tracing_subscriber::EnvFilter::from_default_env(), + ) + .with_writer(std::io::stderr) + .init(); + } + }; let mut args = std::env::args_os(); let config_path = args.nth(1).map(PathBuf::from);