From: Gabriel Goller <g.goller@proxmox.com>
To: pve-devel@lists.proxmox.com
Cc: Lukas Wagner <l.wagner@proxmox.com>
Subject: [pve-devel] [PATCH proxmox 2/2] log: add layer for pve workertasks in perlmod crates
Date: Tue, 18 Feb 2025 17:13:09 +0100 [thread overview]
Message-ID: <20250218161311.558674-3-g.goller@proxmox.com> (raw)
In-Reply-To: <20250218161311.558674-1-g.goller@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
next prev parent reply other threads:[~2025-02-18 16:13 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-02-18 16:13 [pve-devel] [PATCH proxmox{, -backup, -perl-rs} 0/4] Introduce builder for logging initialization Gabriel Goller
2025-02-18 16:13 ` [pve-devel] [PATCH proxmox 1/2] log: introduce logging builder Gabriel Goller
2025-02-18 16:13 ` Gabriel Goller [this message]
2025-02-18 16:13 ` [pve-devel] [PATCH proxmox-backup 1/1] log: use new builder initializer Gabriel Goller
2025-02-18 16:13 ` [pve-devel] [PATCH proxmox-perl-rs 1/1] log: use new logging builder, print to stderr and journald Gabriel Goller
2025-02-18 16:16 ` [pve-devel] [PATCH proxmox{, -backup, -perl-rs} 0/4] Introduce builder for logging initialization 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=20250218161311.558674-3-g.goller@proxmox.com \
--to=g.goller@proxmox.com \
--cc=l.wagner@proxmox.com \
--cc=pve-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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal