From fe8a7819fa7133d05d3973079119c85cf7d77f58 Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Wed, 20 Mar 2024 14:21:08 +0100 Subject: [syslog] Add support to logging to syslog Original patch by Jakub Jirutka for Alpine Linux port. --- src/garage/main.rs | 58 +++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 47 insertions(+), 11 deletions(-) (limited to 'src/garage/main.rs') diff --git a/src/garage/main.rs b/src/garage/main.rs index e489fff0..3fe96338 100644 --- a/src/garage/main.rs +++ b/src/garage/main.rs @@ -140,17 +140,8 @@ async fn main() { let opt = Opt::from_clap(&Opt::clap().version(version.as_str()).get_matches()); // Initialize logging as well as other libraries used in Garage - if std::env::var("RUST_LOG").is_err() { - let default_log = match &opt.cmd { - Command::Server => "netapp=info,garage=info", - _ => "netapp=warn,garage=warn", - }; - 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(); + init_logging(&opt); + sodiumoxide::init().expect("Unable to init sodiumoxide"); let res = match opt.cmd { @@ -173,6 +164,51 @@ async fn main() { } } +fn init_logging(opt: &Opt) { + if std::env::var("RUST_LOG").is_err() { + let default_log = match &opt.cmd { + Command::Server => "netapp=info,garage=info", + _ => "netapp=warn,garage=warn", + }; + std::env::set_var("RUST_LOG", default_log) + } + + let env_filter = tracing_subscriber::filter::EnvFilter::from_default_env(); + + #[cfg(feature = "syslog")] + if std::env::var("GARAGE_LOG_TO_SYSLOG") + .map(|x| x == "1" || x == "true") + .unwrap_or(false) + { + 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(); + + return; + } + + tracing_subscriber::fmt() + .with_writer(std::io::stderr) + .with_env_filter(env_filter) + .init(); +} + async fn cli_command(opt: Opt) -> Result<(), Error> { let config = if (opt.secrets.rpc_secret.is_none() && opt.secrets.rpc_secret_file.is_none()) || opt.rpc_host.is_none() -- cgit v1.2.3 From 84018be8622b582ad8853df042d10f0e122e6316 Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Wed, 20 Mar 2024 14:36:13 +0100 Subject: [syslog] warning when syslog support is not enabled --- src/garage/main.rs | 51 +++++++++++++++++++++++++++++---------------------- 1 file changed, 29 insertions(+), 22 deletions(-) (limited to 'src/garage/main.rs') diff --git a/src/garage/main.rs b/src/garage/main.rs index 3fe96338..2f9ae508 100644 --- a/src/garage/main.rs +++ b/src/garage/main.rs @@ -175,32 +175,39 @@ fn init_logging(opt: &Opt) { let env_filter = tracing_subscriber::filter::EnvFilter::from_default_env(); - #[cfg(feature = "syslog")] if std::env::var("GARAGE_LOG_TO_SYSLOG") .map(|x| x == "1" || x == "true") .unwrap_or(false) { - 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(); - - return; + #[cfg(feature = "syslog")] + { + 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(); + + return; + } + #[cfg(not(feature = "syslog"))] + { + eprintln!("Syslog support is not enabled in this build."); + std::process::exit(1); + } } tracing_subscriber::fmt() -- cgit v1.2.3