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.wag...@proxmox.com>
Signed-off-by: Gabriel Goller <g.gol...@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

Reply via email to