From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: <pve-devel-bounces@lists.proxmox.com> Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) by lore.proxmox.com (Postfix) with ESMTPS id 518961FF189 for <inbox@lore.proxmox.com>; Tue, 18 Feb 2025 17:13:51 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id EE829116BF; Tue, 18 Feb 2025 17:13:47 +0100 (CET) From: Gabriel Goller <g.goller@proxmox.com> To: pve-devel@lists.proxmox.com Date: Tue, 18 Feb 2025 17:13:09 +0100 Message-Id: <20250218161311.558674-3-g.goller@proxmox.com> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250218161311.558674-1-g.goller@proxmox.com> References: <20250218161311.558674-1-g.goller@proxmox.com> MIME-Version: 1.0 X-SPAM-LEVEL: Spam detection results: 0 AWL -0.032 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 RCVD_IN_VALIDITY_CERTIFIED_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to Validity was blocked. See https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more information. RCVD_IN_VALIDITY_RPBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to Validity was blocked. See https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more information. RCVD_IN_VALIDITY_SAFE_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to Validity was blocked. See https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more information. SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [builder.rs, lib.rs] Subject: [pve-devel] [PATCH proxmox 2/2] log: add layer for pve workertasks in perlmod crates X-BeenThere: pve-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox VE development discussion <pve-devel.lists.proxmox.com> List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pve-devel>, <mailto:pve-devel-request@lists.proxmox.com?subject=unsubscribe> List-Archive: <http://lists.proxmox.com/pipermail/pve-devel/> List-Post: <mailto:pve-devel@lists.proxmox.com> List-Help: <mailto:pve-devel-request@lists.proxmox.com?subject=help> List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel>, <mailto:pve-devel-request@lists.proxmox.com?subject=subscribe> Reply-To: Proxmox VE development discussion <pve-devel@lists.proxmox.com> Cc: Lukas Wagner <l.wagner@proxmox.com> Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: pve-devel-bounces@lists.proxmox.com Sender: "pve-devel" <pve-devel-bounces@lists.proxmox.com> Add a layer that outputs messages to stderr in a specific format. In PVE, stderr is rerouted to the tasklog if the we are within a workertask. Therefore, ensure the stderr output is formatted appropriately. Reported-by: Lukas Wagner <l.wagner@proxmox.com> Signed-off-by: Gabriel Goller <g.goller@proxmox.com> --- proxmox-log/src/builder.rs | 16 +++++++++++++- proxmox-log/src/lib.rs | 1 + proxmox-log/src/pve_task_formatter.rs | 31 +++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 proxmox-log/src/pve_task_formatter.rs diff --git a/proxmox-log/src/builder.rs b/proxmox-log/src/builder.rs index f7db38a94982..5725b4c0f09d 100644 --- a/proxmox-log/src/builder.rs +++ b/proxmox-log/src/builder.rs @@ -4,7 +4,7 @@ use tracing_subscriber::{filter::filter_fn, layer::SubscriberExt, Layer}; use crate::{ get_env_variable, journald_or_stderr_layer, plain_stderr_layer, - tasklog_layer::TasklogLayer, LogContext, + pve_task_formatter::PveTaskFormatter, tasklog_layer::TasklogLayer, LogContext, }; /// Builder-like struct to compose your logging layers. @@ -114,6 +114,20 @@ impl Logger { self } + /// Print to stderr in the PVE format. + /// + /// The PVE format only prints the event level and messages. + /// e.g.: `DEBUG: event message`. + pub fn stderr_pve(mut self) -> Logger { + let layer = tracing_subscriber::fmt::layer() + .event_format(PveTaskFormatter {}) + .with_writer(std::io::stderr) + .with_filter(self.global_log_level) + .boxed(); + self.layer.push(layer); + self + } + /// Inits the tracing logger with the previously configured layers. /// /// Also configures the `LogTracer` which will convert all `log` events to tracing events. diff --git a/proxmox-log/src/lib.rs b/proxmox-log/src/lib.rs index 51ca89acc992..2f4d4dabe23c 100644 --- a/proxmox-log/src/lib.rs +++ b/proxmox-log/src/lib.rs @@ -9,6 +9,7 @@ use tokio::task::futures::TaskLocalFuture; use tracing_subscriber::prelude::*; mod file_logger; +mod pve_task_formatter; mod tasklog_layer; pub mod builder; diff --git a/proxmox-log/src/pve_task_formatter.rs b/proxmox-log/src/pve_task_formatter.rs new file mode 100644 index 000000000000..e9866a4b0869 --- /dev/null +++ b/proxmox-log/src/pve_task_formatter.rs @@ -0,0 +1,31 @@ +use std::fmt; +use tracing::{Event, Subscriber}; +use tracing_subscriber::field::VisitOutput; +use tracing_subscriber::fmt::format::{DefaultVisitor, Writer}; +use tracing_subscriber::fmt::{FmtContext, FormatEvent, FormatFields}; +use tracing_subscriber::registry::LookupSpan; + +/// This custom formatter outputs logs as they are visible in the PVE task log. +/// +/// e.g.: "DEBUG: sample message" +pub struct PveTaskFormatter {} + +impl<C, N> FormatEvent<C, N> for PveTaskFormatter +where + C: Subscriber + for<'a> LookupSpan<'a>, + N: for<'a> FormatFields<'a> + 'static, +{ + fn format_event( + &self, + _ctx: &FmtContext<'_, C, N>, + mut writer: Writer<'_>, + event: &Event<'_>, + ) -> fmt::Result { + write!(writer, "{}: ", event.metadata().level().as_str())?; + + let mut v = DefaultVisitor::new(writer.by_ref(), true); + event.record(&mut v); + v.finish()?; + writer.write_char('\n') + } +} -- 2.39.5 _______________________________________________ pve-devel mailing list pve-devel@lists.proxmox.com https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel