From: Jakub Jirutka Date: Sun, 07 May 2023 00:08:59 +0200 Subject: [PATCH] Add support for logging into syslog and don't output ANSI sequences (colours) This is more a quick&dirty approach, not meant to be upstreamed as-is. --- a/src/garage/main.rs +++ b/src/garage/main.rs @@ -156,10 +156,36 @@ }; std::env::set_var("RUST_LOG", default_log) } - tracing_subscriber::fmt() - .with_writer(std::io::stderr) - .with_env_filter(tracing_subscriber::filter::EnvFilter::from_default_env()) - .init(); + + let env_filter = tracing_subscriber::filter::EnvFilter::from_default_env(); + + if std::env::var("GARAGE_SYSLOG").is_ok() { + use std::ffi::CStr; + use syslog_tracing::{Facility, Options, Syslog}; + + let syslog = Syslog::new( + CStr::from_bytes_with_nul(b"garage\0").unwrap(), + Options::LOG_PID | Options::LOG_PERROR, + Facility::Daemon, + ).expect("Unable to init syslog"); + + tracing_subscriber::fmt() + .with_writer(syslog) + .with_env_filter(env_filter) + .with_ansi(false) // disable ANSI escape sequences (colours) + .with_file(false) + .with_level(false) + .without_time() + .compact() + .init(); + } else { + tracing_subscriber::fmt() + .with_writer(std::io::stderr) + .with_env_filter(env_filter) + .with_ansi(false) // disable ANSI escape sequences (colours) + .init(); + } + sodiumoxide::init().expect("Unable to init sodiumoxide"); let res = match opt.cmd { --- a/src/garage/Cargo.toml +++ b/src/garage/Cargo.toml @@ -58,6 +58,8 @@ opentelemetry-otlp = { version = "0.10", optional = true } prometheus = { version = "0.13", optional = true } +syslog-tracing = "0.1.0" + [dev-dependencies] aws-sdk-s3 = "0.19" chrono = "0.4" --- a/Cargo.lock +++ b/Cargo.lock @@ -1113,6 +1113,7 @@ "sha2 0.10.6", "static_init", "structopt", + "syslog-tracing", "timeago", "tokio", "toml", @@ -3559,6 +3560,17 @@ "quote", "syn", "unicode-xid", +] + +[[package]] +name = "syslog-tracing" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4cf9ff1f9f8e5ba220a58bbccb0375f4cabd785f96a8683b31389cefd91be747" +dependencies = [ + "libc", + "tracing-core", + "tracing-subscriber", ] [[package]]