From 4a0f3b360b68f53be74c0698699f73787db1894e Mon Sep 17 00:00:00 2001 From: Timshel Date: Thu, 1 Feb 2024 17:32:15 +0100 Subject: [PATCH] Allow to override log level for specific target --- .env.template | 7 +++++++ src/config.rs | 23 +++++++++++++++++++++++ src/main.rs | 4 ++++ 3 files changed, 34 insertions(+) diff --git a/.env.template b/.env.template index a7cfe1133d..e55f15c32e 100644 --- a/.env.template +++ b/.env.template @@ -370,6 +370,13 @@ ## routes and static file, websocket and alive requests # LOG_LEVEL=info +## log level target override +## Change the verbosity of specific log output +## Format is a line for each "target=log_level" +#LOG_LEVEL_OVERRIDE=" +#routes=warn +#" + ## Token for the admin interface, preferably an Argon2 PCH string ## Vaultwarden has a built-in generator by calling `vaultwarden hash` ## For details see: https://github.com/dani-garcia/vaultwarden/wiki/Enabling-admin-page#secure-the-admin_token diff --git a/src/config.rs b/src/config.rs index 0ab3a1df72..d35a77399c 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,8 +1,10 @@ use std::env::consts::EXE_SUFFIX; use std::process::exit; +use std::str::FromStr; use std::sync::RwLock; use job_scheduler_ng::Schedule; +use log::LevelFilter; use once_cell::sync::Lazy; use reqwest::Url; @@ -571,6 +573,8 @@ make_config! { log_file: String, false, option; /// Log level log_level: String, false, def, "Info".to_string(); + /// Override individual log level + log_level_override: String, false, def, String::new(); /// Enable DB WAL |> Turning this off might lead to worse performance, but might help if using vaultwarden on some exotic filesystems, /// that do not support WAL. Please make sure you read project wiki on the topic before changing this setting. @@ -1343,6 +1347,25 @@ impl Config { pub fn sso_scopes_vec(&self) -> Vec { self.sso_scopes().split_whitespace().map(str::to_string).collect() } + + pub fn log_overrides(&self) -> Vec<(String, LevelFilter)> { + self.log_level_override() + .lines() + .map(|l| l.trim()) + .filter(|l| !l.is_empty() && !l.starts_with("//")) + .filter_map(|l| { + let split = l.split('=').collect::>(); + let log = match &split[..] { + [path, level] => LevelFilter::from_str(level).ok().map(|l| ((*path).to_string(), l)), + _ => None, + }; + if log.is_none() { + println!("[WARNING] Failed to parse ({l}) as a log override. Expected xxx=log_level"); + } + log + }) + .collect() + } } use handlebars::{ diff --git a/src/main.rs b/src/main.rs index e5d92cc629..8a88b5c903 100644 --- a/src/main.rs +++ b/src/main.rs @@ -314,6 +314,10 @@ fn init_logging(level: log::LevelFilter) -> Result<(), fern::InitError> { logger = logger.level_for("lettre::transport::smtp", log::LevelFilter::Off) } + for (path, level) in CONFIG.log_overrides() { + logger = logger.level_for(path, level); + } + if CONFIG.extended_logging() { logger = logger.format(|out, message, record| { out.finish(format_args!(