mirror of
https://git.proxmox.com/git/proxmox
synced 2025-04-28 17:08:47 +00:00
log: add layer for pve workertasks in perlmod crates
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>
This commit is contained in:
parent
f6269b800d
commit
656fedb0c4
@ -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.
|
||||
|
@ -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;
|
||||
|
31
proxmox-log/src/pve_task_formatter.rs
Normal file
31
proxmox-log/src/pve_task_formatter.rs
Normal file
@ -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')
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user