From: Gabriel Goller <g.goller@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [pbs-devel] [PATCH] add tracing init_cli_logger and remove old one
Date: Wed, 21 Aug 2024 12:19:18 +0200 [thread overview]
Message-ID: <20240821101919.217238-1-g.goller@proxmox.com> (raw)
Remove 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>
---
proxmox-log/src/lib.rs | 37 ++++++++++++++++++++++++++++++++++-
proxmox-router/src/cli/mod.rs | 13 ------------
2 files changed, 36 insertions(+), 14 deletions(-)
diff --git a/proxmox-log/src/lib.rs b/proxmox-log/src/lib.rs
index 796d10145b9e..806d16a68d32 100644
--- a/proxmox-log/src/lib.rs
+++ b/proxmox-log/src/lib.rs
@@ -8,7 +8,8 @@ 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;
+pub use tracing_subscriber::filter::LevelFilter;
use tracing_subscriber::prelude::*;
use tasklog_layer::TasklogLayer;
@@ -125,3 +126,37 @@ 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) {
+ if let Ok(l) = v.parse::<LevelFilter>() {
+ log_level = l;
+ }
+ }
+
+ 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..148edb467494 100644
--- a/proxmox-router/src/cli/mod.rs
+++ b/proxmox-router/src/cli/mod.rs
@@ -57,19 +57,6 @@ pub use readline::*;
/// return a list of all possible values.
pub type CompletionFunction = fn(&str, &HashMap<String, String>) -> Vec<String>;
-/// Initialize default logger for CLI binaries
-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),
- )
- .write_style(env_logger::WriteStyle::Never)
- .format_level(false)
- .format_module_path(false)
- .format_target(false)
- .format_timestamp(None)
- .init();
-}
-
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
/// Use for simple yes or no questions, where booleans can be confusing, especially if there's a
/// default response to consider. The implementation provides query helper for the CLI.
--
2.39.2
_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel
next reply other threads:[~2024-08-21 10:19 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-08-21 10:19 Gabriel Goller [this message]
2024-08-21 10:19 ` [pbs-devel] [PATCH proxmox-backup] move client binaries to tracing Gabriel Goller
2024-08-29 12:07 ` [pbs-devel] [PATCH] add tracing init_cli_logger and remove old one Wolfgang Bumiller
2024-08-29 13:39 ` Gabriel Goller
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240821101919.217238-1-g.goller@proxmox.com \
--to=g.goller@proxmox.com \
--cc=pbs-devel@lists.proxmox.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox