From 72bbe39bab530b0444e0207064e8147d9eebe4ef Mon Sep 17 00:00:00 2001 From: Dhruvin Gandhi Date: Fri, 14 Apr 2023 23:25:13 +0530 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 | 51 +++++++++++++++++++++++++++++++++++++++ meilisearch/Cargo.toml | 1 + meilisearch/src/main.rs | 17 ++++++++++--- meilisearch/src/option.rs | 8 ++++++ 4 files changed, 74 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 853d1a89..5f0038a7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1337,6 +1337,15 @@ dependencies = [ "libc", ] +[[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.8.0" @@ -1795,6 +1804,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" @@ -2437,6 +2457,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 = "md5" version = "0.7.0" @@ -2523,6 +2549,7 @@ dependencies = [ "slice-group-by", "static-files", "sysinfo", + "syslog", "tar", "temp-env", "tempfile", @@ -2802,6 +2829,15 @@ dependencies = [ "libc", ] +[[package]] +name = "num_threads" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2819ce041d2ee131036f4fc9d6ae7ae125a3a40e97ba64d04fe799ad9dabbb44" +dependencies = [ + "libc", +] + [[package]] name = "object" version = "0.27.1" @@ -3765,6 +3801,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", +] + [[package]] name = "tar" version = "0.4.38" @@ -3841,6 +3890,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a561bf4617eebd33bca6434b988f39ed798e527f51a1e797d0ee4f61c0a38376" dependencies = [ "itoa 1.0.5", + "libc", + "num_threads", "serde", "time-core", "time-macros", diff --git a/meilisearch/Cargo.toml b/meilisearch/Cargo.toml index d4b77232..5ce311e7 100644 --- a/meilisearch/Cargo.toml +++ b/meilisearch/Cargo.toml @@ -81,6 +81,7 @@ serde_urlencoded = "0.7.1" actix-utils = "3.0.1" atty = "0.2.14" termcolor = "1.1.3" +syslog = "6.0.1" [dev-dependencies] actix-rt = "2.7.0" diff --git a/meilisearch/src/main.rs b/meilisearch/src/main.rs index d12539e2..132c97f6 100644 --- a/meilisearch/src/main.rs +++ b/meilisearch/src/main.rs @@ -17,10 +17,21 @@ 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.to_string()); + if opt.syslog { + let level = opt + .log_level + .to_string() + .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.to_string()); - log_builder.init(); + log_builder.init(); + } Ok(()) } diff --git a/meilisearch/src/option.rs b/meilisearch/src/option.rs index 0c6457e7..b2335a78 100644 --- a/meilisearch/src/option.rs +++ b/meilisearch/src/option.rs @@ -47,6 +47,7 @@ const MEILI_IGNORE_MISSING_DUMP: &str = "MEILI_IGNORE_MISSING_DUMP"; const MEILI_IGNORE_DUMP_IF_DB_EXISTS: &str = "MEILI_IGNORE_DUMP_IF_DB_EXISTS"; const MEILI_DUMP_DIR: &str = "MEILI_DUMP_DIR"; const MEILI_LOG_LEVEL: &str = "MEILI_LOG_LEVEL"; +const MEILI_SYSLOG: &str = "MEILI_SYSLOG"; const MEILI_EXPERIMENTAL_ENABLE_METRICS: &str = "MEILI_EXPERIMENTAL_ENABLE_METRICS"; const DEFAULT_CONFIG_FILE_PATH: &str = "./config.toml"; @@ -286,6 +287,11 @@ pub struct Opt { #[serde(default)] pub log_level: LogLevel, + /// Send log messages to the local syslog instead of stderr. + #[clap(long, env = MEILI_SYSLOG)] + #[serde(default)] + pub syslog: bool, + /// Experimental metrics feature. For more information, see: /// /// Enables the Prometheus metrics on the `GET /metrics` endpoint. @@ -374,6 +380,7 @@ impl Opt { schedule_snapshot, dump_dir, log_level, + syslog, indexer_options, import_snapshot: _, ignore_missing_snapshot: _, @@ -422,6 +429,7 @@ impl Opt { export_to_env_if_not_present(MEILI_DUMP_DIR, dump_dir); export_to_env_if_not_present(MEILI_LOG_LEVEL, log_level.to_string()); + export_to_env_if_not_present(MEILI_SYSLOG, syslog.to_string()); export_to_env_if_not_present( MEILI_EXPERIMENTAL_ENABLE_METRICS, enable_metrics_route.to_string(), -- 2.40.0