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 <l.wagner@proxmox.com>
This commit is contained in:
Lukas Wagner 2023-05-24 15:56:25 +02:00 committed by Wolfgang Bumiller
parent 15e7531f3c
commit d0cab6371a
3 changed files with 12 additions and 4 deletions

View File

@ -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}");
}
}

View File

@ -12,6 +12,6 @@ mod export {
#[export]
pub fn init() {
common::logger::init();
common::logger::init("PMG_LOG", "info");
}
}

View File

@ -14,6 +14,6 @@ mod export {
#[export]
pub fn init() {
common::logger::init();
common::logger::init("PVE_LOG", "info");
}
}