Patch-Source: https://github.com/meilisearch/meilisearch/pull/2812 -- From 97f3f12305becc94adc54883c8ac9a815f51b380 Mon Sep 17 00:00:00 2001 From: Jakub Jirutka Date: Wed, 28 Sep 2022 23:50:21 +0200 Subject: [PATCH] Add support for logging to syslog If `MEILI_SYSLOG` is set or `--syslog` provided, the log messages will be sent to the local syslog instead of the stderr. The log level can be controlled using `MEILI_LOG_LEVEL` (or `--log-level`) as with logging to the stderr, but the value must be a valid syslog level, otherwise the program will panic. --- Cargo.lock | 40 ++++++++++++++++++++++++++++++++++ meilisearch-http/Cargo.toml | 1 + meilisearch-http/src/main.rs | 24 ++++++++++++++------ meilisearch-http/src/option.rs | 4 ++++ 4 files changed, 62 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7a0802e3e..4079aa5c9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1096,6 +1096,15 @@ dependencies = [ "termcolor", ] +[[package]] +name = "error-chain" +version = "0.12.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2d2f06b9cac1506ece98fe3231e3cc9c4410ec3d5b1f24ae1c8946f0742cdefc" +dependencies = [ + "version_check", +] + [[package]] name = "fastrand" version = "1.7.0" @@ -1487,6 +1496,17 @@ dependencies = [ "digest", ] +[[package]] +name = "hostname" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c731c3e10504cc8ed35cfe2f1db4c9274c3d35fa486e3b31df46f068ef3e867" +dependencies = [ + "libc", + "match_cfg", + "winapi", +] + [[package]] name = "http" version = "0.2.8" @@ -1997,6 +2017,12 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3e2e65a1a2e43cfcb47a895c4c8b10d1f4a61097f9f254f183aee60cad9c651d" +[[package]] +name = "match_cfg" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ffbee8634e0d45d258acb448e7eaab3fce7a0a467395d4d9f228e3c1f01fb2e4" + [[package]] name = "matches" version = "0.1.9" @@ -2081,6 +2107,7 @@ dependencies = [ "slice-group-by", "static-files", "sysinfo", + "syslog", "tar", "tempfile", "thiserror", @@ -3382,6 +3409,19 @@ dependencies = [ "winapi", ] +[[package]] +name = "syslog" +version = "6.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "978044cc68150ad5e40083c9f6a725e6fd02d7ba1bcf691ec2ff0d66c0b41acc" +dependencies = [ + "error-chain", + "hostname", + "libc", + "log", + "time 0.3.14", +] + [[package]] name = "tar" version = "0.4.38" diff --git a/meilisearch-http/Cargo.toml b/meilisearch-http/Cargo.toml index baea8b578..f60b16fae 100644 --- a/meilisearch-http/Cargo.toml +++ b/meilisearch-http/Cargo.toml @@ -70,6 +70,7 @@ siphasher = "0.3.10" slice-group-by = "0.3.0" static-files = { version = "0.2.3", optional = true } sysinfo = "0.23.5" +syslog = "6.0.1" tar = "0.4.38" tempfile = "3.3.0" thiserror = "1.0.30" diff --git a/meilisearch-http/src/main.rs b/meilisearch-http/src/main.rs index 9627aeef8..a13ccd6ce 100644 --- a/meilisearch-http/src/main.rs +++ b/meilisearch-http/src/main.rs @@ -15,14 +15,24 @@ static ALLOC: mimalloc::MiMalloc = mimalloc::MiMalloc; /// does all the setup before meilisearch is launched fn setup(opt: &Opt) -> anyhow::Result<()> { - let mut log_builder = env_logger::Builder::new(); - log_builder.parse_filters(&opt.log_level); - if opt.log_level == "info" { - // if we are in info we only allow the warn log_level for milli - log_builder.filter_module("milli", log::LevelFilter::Warn); - } + if opt.syslog { + let level = opt.log_level.parse().expect(&format!( + "MEILI_LOG_LEVEL={} is not a valid syslog level", + opt.log_level + )); + syslog::init_unix(syslog::Facility::LOG_DAEMON, level) + .expect("Failed to connect to syslog"); + eprintln!("Logging to syslog"); + } else { + let mut log_builder = env_logger::Builder::new(); + log_builder.parse_filters(&opt.log_level); + if opt.log_level == "info" { + // if we are in info we only allow the warn log_level for milli + log_builder.filter_module("milli", log::LevelFilter::Warn); + } - log_builder.init(); + log_builder.init(); + } Ok(()) } diff --git a/meilisearch-http/src/option.rs b/meilisearch-http/src/option.rs index bdfa283a6..6484197cb 100644 --- a/meilisearch-http/src/option.rs +++ b/meilisearch-http/src/option.rs @@ -146,6 +146,10 @@ pub struct Opt { #[clap(long, env = "MEILI_LOG_LEVEL", default_value = "info")] pub log_level: String, + /// Send log messages to the local syslog instead of stderr. + #[clap(long, env = "MEILI_SYSLOG")] + pub syslog: bool, + /// Enables Prometheus metrics and /metrics route. #[cfg(feature = "metrics")] #[clap(long, env = "MEILI_ENABLE_METRICS_ROUTE")]