From d0cab6371a674acc492e103e55d4305a667104af Mon Sep 17 00:00:00 2001 From: Lukas Wagner Date: Wed, 24 May 2023 15:56:25 +0200 Subject: [PATCH] log: set default log level to 'info', add product specific logging env var Logging behaviour can be overridden by the {PMG,PVE}_LOG environment variable. This commit also disables styled output and timestamps in log messages, since we usually log to the journal anyway. The log output is configured to match with other log messages in task logs. Signed-off-by: Lukas Wagner --- common/src/logger.rs | 12 ++++++++++-- pmg-rs/src/lib.rs | 2 +- pve-rs/src/lib.rs | 2 +- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/common/src/logger.rs b/common/src/logger.rs index 36dc856..3c9a075 100644 --- a/common/src/logger.rs +++ b/common/src/logger.rs @@ -1,6 +1,14 @@ +use env_logger::{Builder, Env}; +use std::io::Write; + /// Initialize logging. Should only be called once -pub fn init() { - if let Err(e) = env_logger::try_init() { +pub fn init(env_var_name: &str, default_log_level: &str) { + if let Err(e) = Builder::from_env(Env::new().filter_or(env_var_name, default_log_level)) + .format(|buf, record| writeln!(buf, "{}: {}", record.level(), record.args())) + .write_style(env_logger::WriteStyle::Never) + .format_timestamp(None) + .try_init() + { eprintln!("could not set up env_logger: {e}"); } } diff --git a/pmg-rs/src/lib.rs b/pmg-rs/src/lib.rs index 8633136..6b7ee4c 100644 --- a/pmg-rs/src/lib.rs +++ b/pmg-rs/src/lib.rs @@ -12,6 +12,6 @@ mod export { #[export] pub fn init() { - common::logger::init(); + common::logger::init("PMG_LOG", "info"); } } diff --git a/pve-rs/src/lib.rs b/pve-rs/src/lib.rs index fc31b3a..eb6ae02 100644 --- a/pve-rs/src/lib.rs +++ b/pve-rs/src/lib.rs @@ -14,6 +14,6 @@ mod export { #[export] pub fn init() { - common::logger::init(); + common::logger::init("PVE_LOG", "info"); } }