From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <pbs-devel-bounces@lists.proxmox.com>
Received: from firstgate.proxmox.com (firstgate.proxmox.com [IPv6:2a01:7e0:0:424::9])
	by lore.proxmox.com (Postfix) with ESMTPS id A44261FF163
	for <inbox@lore.proxmox.com>; Thu, 29 Aug 2024 15:40:17 +0200 (CEST)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
	by firstgate.proxmox.com (Proxmox) with ESMTP id B13751CB80;
	Thu, 29 Aug 2024 15:40:46 +0200 (CEST)
From: Gabriel Goller <g.goller@proxmox.com>
To: pbs-devel@lists.proxmox.com
Date: Thu, 29 Aug 2024 15:40:39 +0200
Message-Id: <20240829134040.335400-1-g.goller@proxmox.com>
X-Mailer: git-send-email 2.39.2
MIME-Version: 1.0
X-SPAM-LEVEL: Spam detection results:  0
 AWL -0.041 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
 T_SCC_BODY_TEXT_LINE    -0.01 -
Subject: [pbs-devel] [PATCH v2] add tracing init_cli_logger and deprecate
 old one
X-BeenThere: pbs-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Proxmox Backup Server development discussion
 <pbs-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pbs-devel>, 
 <mailto:pbs-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pbs-devel/>
List-Post: <mailto:pbs-devel@lists.proxmox.com>
List-Help: <mailto:pbs-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel>, 
 <mailto:pbs-devel-request@lists.proxmox.com?subject=subscribe>
Reply-To: Proxmox Backup Server development discussion
 <pbs-devel@lists.proxmox.com>
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: 7bit
Errors-To: pbs-devel-bounces@lists.proxmox.com
Sender: "pbs-devel" <pbs-devel-bounces@lists.proxmox.com>

Deprecate the proxmox-router init_cli_logger function used in client
binaries such as `proxmox-backup-client`, `proxmox-backup-manager`,
'pxar', etc... Add a new init_cli_logger function that uses tracing
instead of env_logger. It checks if the task is in a workertask and
prints the message either to stdout or to the tasklog (this is
neccessary for commands in proxmox-backup-manager that call api handlers
that start workerthreads from the client).

Signed-off-by: Gabriel Goller <g.goller@proxmox.com>
---

v2, thanks @Wolfgang:
 - order imports
 - don't delete old function, but deprecate
 - add stderr warning when log level is not parsable

 proxmox-log/src/lib.rs        | 42 ++++++++++++++++++++++++++++++++++-
 proxmox-router/src/cli/mod.rs |  1 +
 2 files changed, 42 insertions(+), 1 deletion(-)

diff --git a/proxmox-log/src/lib.rs b/proxmox-log/src/lib.rs
index 796d10145b9e..bcde5495dead 100644
--- a/proxmox-log/src/lib.rs
+++ b/proxmox-log/src/lib.rs
@@ -8,7 +8,7 @@ use std::sync::{Arc, Mutex};
 use tokio::task::futures::TaskLocalFuture;
 use tracing::Level;
 use tracing_log::{AsLog, LogTracer};
-use tracing_subscriber::filter::{filter_fn, LevelFilter};
+use tracing_subscriber::filter::filter_fn;
 use tracing_subscriber::prelude::*;
 
 use tasklog_layer::TasklogLayer;
@@ -32,6 +32,7 @@ pub use tracing::trace;
 pub use tracing::trace_span;
 pub use tracing::warn;
 pub use tracing::warn_span;
+pub use tracing_subscriber::filter::LevelFilter;
 
 tokio::task_local! {
     static LOG_CONTEXT: LogContext;
@@ -125,3 +126,42 @@ impl LogContext {
         &self.logger
     }
 }
+
+/// Initialize default logger for CLI binaries
+pub fn init_cli_logger(
+    env_var_name: &str,
+    default_log_level: LevelFilter,
+) -> Result<(), anyhow::Error> {
+    let mut log_level = default_log_level;
+    if let Ok(v) = env::var(env_var_name) {
+        match v.parse::<LevelFilter>() {
+            Ok(l) => {
+                log_level = l;
+            },
+            Err(e) => {
+                eprintln!("env variable PBS_LOG found, but parsing failed: {e:?}");
+            }
+        }
+    }
+
+    let format = tracing_subscriber::fmt::format()
+        .with_level(false)
+        .without_time()
+        .with_target(false)
+        .compact();
+
+    let registry = tracing_subscriber::registry()
+        .with(
+            tracing_subscriber::fmt::layer()
+                .event_format(format)
+                .with_filter(filter_fn(|metadata| {
+                    !LogContext::exists() || *metadata.level() >= Level::ERROR
+                }))
+                .with_filter(log_level),
+        )
+        .with(TasklogLayer {}.with_filter(log_level));
+
+    tracing::subscriber::set_global_default(registry)?;
+    LogTracer::init_with_filter(log_level.as_log())?;
+    Ok(())
+}
diff --git a/proxmox-router/src/cli/mod.rs b/proxmox-router/src/cli/mod.rs
index 3cc41ab3ea94..2b5a69c83dce 100644
--- a/proxmox-router/src/cli/mod.rs
+++ b/proxmox-router/src/cli/mod.rs
@@ -58,6 +58,7 @@ pub use readline::*;
 pub type CompletionFunction = fn(&str, &HashMap<String, String>) -> Vec<String>;
 
 /// Initialize default logger for CLI binaries
+#[deprecated = "use proxmox_log::init_cli_logger instead"]
 pub fn init_cli_logger(env_var_name: &str, default_log_level: &str) {
     env_logger::Builder::from_env(
         env_logger::Env::new().filter_or(env_var_name, default_log_level),
-- 
2.39.2



_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel