From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id 73B44D7E2 for ; Mon, 21 Aug 2023 13:20:13 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 5B1671658E for ; Mon, 21 Aug 2023 13:19:43 +0200 (CEST) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [94.136.29.106]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS for ; Mon, 21 Aug 2023 13:19:42 +0200 (CEST) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 8140942CFC for ; Mon, 21 Aug 2023 13:19:42 +0200 (CEST) From: Gabriel Goller To: pbs-devel@lists.proxmox.com Date: Mon, 21 Aug 2023 13:19:38 +0200 Message-Id: <20230821111938.110298-2-g.goller@proxmox.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230821111938.110298-1-g.goller@proxmox.com> References: <20230821111938.110298-1-g.goller@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL -0.482 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Subject: [pbs-devel] [PATCH proxmox] router: Added `init_syslog_logger()` function X-BeenThere: pbs-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Backup Server development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 21 Aug 2023 11:20:13 -0000 This function inits the `syslog` logger and sets the minimal level to the value of the environment variable passed. The default level is `info`. Added the `log` and `syslog` crates. Signed-off-by: Gabriel Goller --- Cargo.toml | 1 + proxmox-router/Cargo.toml | 2 ++ proxmox-router/src/router.rs | 25 +++++++++++++++++++++++-- 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index e334ac1..980a9d8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -84,6 +84,7 @@ url = "2.2" walkdir = "2" webauthn-rs = "0.3" zstd = { version = "0.12", features = [ "bindgen" ] } +syslog = "6" # workspace dependencies proxmox-api-macro = { version = "1.0.5", path = "proxmox-api-macro" } diff --git a/proxmox-router/Cargo.toml b/proxmox-router/Cargo.toml index 1c08ce2..81a8e8c 100644 --- a/proxmox-router/Cargo.toml +++ b/proxmox-router/Cargo.toml @@ -19,6 +19,8 @@ percent-encoding.workspace = true serde_json.workspace = true serde.workspace = true unicode-width ="0.1.8" +syslog = "6" +log = "0.4.17" # cli: tokio = { workspace = true, features = [], optional = true } diff --git a/proxmox-router/src/router.rs b/proxmox-router/src/router.rs index e9a669a..b0a75d7 100644 --- a/proxmox-router/src/router.rs +++ b/proxmox-router/src/router.rs @@ -1,9 +1,9 @@ use std::collections::HashMap; -use std::fmt; use std::future::Future; use std::pin::Pin; +use std::{env, fmt}; -use anyhow::Error; +use anyhow::{anyhow, bail, Error}; #[cfg(feature = "server")] use http::request::Parts; #[cfg(feature = "server")] @@ -580,3 +580,24 @@ impl ApiMethod { self } } + +pub fn init_syslog_logger( + application_name: &str, + env_var_name: &str, + default_log_level: log::LevelFilter, +) -> Result<(), anyhow::Error> { + let mut log_level = default_log_level; + if let Ok(v) = env::var(env_var_name) { + if let Ok(l) = v.parse::() { + log_level = l; + } + } + if let Err(e) = syslog::init( + syslog::Facility::LOG_DAEMON, + log_level, + Some(application_name), + ) { + bail!(e.description().to_owned()); + }; + Ok(()) +} -- 2.39.2