From 3e8565ac25a7cec71e14d291e4ff2fdd2597de94 Mon Sep 17 00:00:00 2001 From: Michael Sloan Date: Fri, 6 Jun 2025 00:49:30 -0600 Subject: [PATCH] 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 --- crates/zlog/src/filter.rs | 10 +++------- crates/zlog/src/zlog.rs | 13 +++++++++---- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/crates/zlog/src/filter.rs b/crates/zlog/src/filter.rs index 7d51df8861..56350d34c3 100644 --- a/crates/zlog/src/filter.rs +++ b/crates/zlog/src/filter.rs @@ -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) { let env_config = ENV_FILTER.get(); let map_new = ScopeMap::new_from_settings_and_env(settings, env_config, DEFAULT_FILTERS); diff --git a/crates/zlog/src/zlog.rs b/crates/zlog/src/zlog.rs index d8b685e57f..570c82314c 100644 --- a/crates/zlog/src/zlog.rs +++ b/crates/zlog/src/zlog.rs @@ -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);