Initialize zlog default filters on init rather than waiting for settings load (#32209)

Now immediately initializes the zlog filter even when there isn't an env
config. Before this change the default filters were applied after
settings load - I was seeing some `zbus` logs on init.

Also defaults to allowing warnings and errors from the suppressed log
sources. If these turn out to be chatty (they don't seem to be so far),
can bring back more suppression.

Release Notes:

- N/A
This commit is contained in:
Michael Sloan 2025-06-06 00:49:30 -06:00 committed by GitHub
parent d801b7b12e
commit 3e8565ac25
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 12 additions and 11 deletions

View file

@ -38,11 +38,11 @@ pub static LEVEL_ENABLED_MAX_CONFIG: AtomicU8 = AtomicU8::new(LEVEL_ENABLED_MAX_
const DEFAULT_FILTERS: &[(&str, log::LevelFilter)] = &[
#[cfg(any(target_os = "linux", target_os = "freebsd"))]
("zbus", log::LevelFilter::Off),
("zbus", log::LevelFilter::Warn),
#[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "windows"))]
("blade_graphics", log::LevelFilter::Off),
("blade_graphics", log::LevelFilter::Warn),
#[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "windows"))]
("naga::back::spv::writer", log::LevelFilter::Off),
("naga::back::spv::writer", log::LevelFilter::Warn),
];
pub fn init_env_filter(filter: env_config::EnvFilter) {
@ -90,10 +90,6 @@ pub fn is_scope_enabled(scope: &Scope, module_path: Option<&str>, level: log::Le
};
}
pub(crate) fn refresh() {
refresh_from_settings(&HashMap::default());
}
pub fn refresh_from_settings(settings: &HashMap<String, String>) {
let env_config = ENV_FILTER.get();
let map_new = ScopeMap::new_from_settings_and_env(settings, env_config, DEFAULT_FILTERS);

View file

@ -5,19 +5,25 @@ mod env_config;
pub mod filter;
pub mod sink;
use anyhow::Context;
pub use sink::{flush, init_output_file, init_output_stdout};
pub const SCOPE_DEPTH_MAX: usize = 4;
pub fn init() {
try_init().expect("Failed to initialize logger");
match try_init() {
Err(err) => {
log::error!("{err}");
eprintln!("{err}");
}
Ok(()) => {}
}
}
pub fn try_init() -> anyhow::Result<()> {
log::set_logger(&ZLOG).context("cannot be initialized twice")?;
log::set_logger(&ZLOG)?;
log::set_max_level(log::LevelFilter::max());
process_env();
filter::refresh_from_settings(&std::collections::HashMap::default());
Ok(())
}
@ -42,7 +48,6 @@ pub fn process_env() {
match env_config::parse(&env_config) {
Ok(filter) => {
filter::init_env_filter(filter);
filter::refresh();
}
Err(err) => {
eprintln!("Failed to parse log filter: {}", err);