[pve-devel] [PATCH proxmox-perl-rs 07/18] log: set default log level to 'info', add product specfig logging env var1

Lukas Wagner l.wagner at proxmox.com
Mon Mar 27 17:18:46 CEST 2023


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 <l.wagner at proxmox.com>
---
 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 5914bc9..d5c1080 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 671aad0..af3076e 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");
     }
 }
-- 
2.30.2






More information about the pve-devel mailing list