Skip to content

Commit

Permalink
Allow to override log level for specific target
Browse files Browse the repository at this point in the history
  • Loading branch information
Timshel committed Jul 10, 2024
1 parent 2dd1627 commit 7147f0e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .env.template
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use std::collections::HashMap;
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;

Expand Down Expand Up @@ -569,6 +571,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.
Expand Down Expand Up @@ -1394,6 +1398,19 @@ impl Config {
pub fn sso_organizations_id_mapping_map(&self) -> HashMap<String, String> {
parse_as_hashmap(self.sso_organizations_id_mapping())
}

pub fn log_overrides(&self) -> Vec<(String, LevelFilter)> {
parse_param_list(self.log_level_override())
.into_iter()
.filter_map(|(k, v)| match LevelFilter::from_str(&v) {
Ok(lv) => Some((k, lv)),
Err(_) => {
println!("[WARNING] Invalid log level: {k}={v}");
None
}
})
.collect()
}
}

use handlebars::{
Expand Down
4 changes: 4 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,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!(
Expand Down

0 comments on commit 7147f0e

Please sign in to comment.