diff --git a/Cargo.lock b/Cargo.lock index 058a5d5517..e02cec3d09 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1016,7 +1016,6 @@ dependencies = [ "tokio", "tower-http", "tracing", - "tracing-subscriber", "walkdir", ] @@ -1035,6 +1034,7 @@ dependencies = [ "tempfile", "toml", "tracing", + "tracing-subscriber", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 4b40754bb6..6a8008e5b9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -66,7 +66,6 @@ toml = "1.0.3" topological-sort = "0.2.2" tower-http = "0.6.8" tracing = "0.1.44" -tracing-subscriber = { version = "0.3.22", features = ["env-filter"] } walkdir = "2.5.0" [package] @@ -96,7 +95,6 @@ mdbook-driver.workspace = true mdbook-html.workspace = true opener.workspace = true tracing.workspace = true -tracing-subscriber.workspace = true # Watch feature ignore = { workspace = true, optional = true } diff --git a/crates/mdbook-core/Cargo.toml b/crates/mdbook-core/Cargo.toml index f1aada8282..c01e4c4752 100644 --- a/crates/mdbook-core/Cargo.toml +++ b/crates/mdbook-core/Cargo.toml @@ -14,6 +14,7 @@ serde.workspace = true serde_json.workspace = true toml.workspace = true tracing.workspace = true +tracing-subscriber = { version = "0.3.22", features = ["env-filter"] } [dev-dependencies] tempfile.workspace = true diff --git a/crates/mdbook-core/src/utils/mod.rs b/crates/mdbook-core/src/utils/mod.rs index 01bd2a6d82..8e7c22b515 100644 --- a/crates/mdbook-core/src/utils/mod.rs +++ b/crates/mdbook-core/src/utils/mod.rs @@ -8,6 +8,49 @@ pub mod fs; mod html; mod toml_ext; +/// Initialize the logger with mdBook's default configuration. +/// +/// This function sets up a tracing subscriber that: +/// - Uses the `MDBOOK_LOG` environment variable for filtering (or defaults to INFO level) +/// - Silences noisy dependencies (handlebars, html5ever) unless explicitly requested +/// - Writes to stderr +/// - Shows the target only when MDBOOK_LOG is set +/// +/// This is useful for preprocessor authors who want consistent logging +/// with mdBook's internal logging. +pub fn init_logger() { + let filter = tracing_subscriber::EnvFilter::builder() + .with_env_var("MDBOOK_LOG") + .with_default_directive(tracing_subscriber::filter::LevelFilter::INFO.into()) + .from_env_lossy(); + let log_env = std::env::var("MDBOOK_LOG"); + // Silence some particularly noisy dependencies unless the user + // specifically asks for them. + let silence_unless_specified = |filter: tracing_subscriber::EnvFilter, target| { + if !log_env.as_ref().map_or(false, |s| { + s.split(',').any(|directive| directive.starts_with(target)) + }) { + filter.add_directive(format!("{target}=warn").parse().unwrap()) + } else { + filter + } + }; + let filter = silence_unless_specified(filter, "handlebars"); + let filter = silence_unless_specified(filter, "html5ever"); + + // Don't show the target by default, since it generally isn't useful + // unless you are overriding the level. + let with_target = log_env.is_ok(); + + tracing_subscriber::fmt() + .without_time() + .with_ansi(std::io::IsTerminal::is_terminal(&std::io::stderr())) + .with_writer(std::io::stderr) + .with_env_filter(filter) + .with_target(with_target) + .init(); +} + pub(crate) use self::toml_ext::TomlExt; pub use self::html::{escape_html, escape_html_attribute}; diff --git a/src/main.rs b/src/main.rs index 68fea9c49c..5e4f636e0a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,7 +16,7 @@ mod cmd; const VERSION: &str = concat!("v", clap::crate_version!()); fn main() { - init_logger(); + utils::init_logger(); let command = create_clap_command(); @@ -90,39 +90,6 @@ fn create_clap_command() -> Command { app } -fn init_logger() { - let filter = tracing_subscriber::EnvFilter::builder() - .with_env_var("MDBOOK_LOG") - .with_default_directive(tracing_subscriber::filter::LevelFilter::INFO.into()) - .from_env_lossy(); - let log_env = std::env::var("MDBOOK_LOG"); - // Silence some particularly noisy dependencies unless the user - // specifically asks for them. - let silence_unless_specified = |filter: tracing_subscriber::EnvFilter, target| { - if !log_env.as_ref().map_or(false, |s| { - s.split(',').any(|directive| directive.starts_with(target)) - }) { - filter.add_directive(format!("{target}=warn").parse().unwrap()) - } else { - filter - } - }; - let filter = silence_unless_specified(filter, "handlebars"); - let filter = silence_unless_specified(filter, "html5ever"); - - // Don't show the target by default, since it generally isn't useful - // unless you are overriding the level. - let with_target = log_env.is_ok(); - - tracing_subscriber::fmt() - .without_time() - .with_ansi(std::io::IsTerminal::is_terminal(&std::io::stderr())) - .with_writer(std::io::stderr) - .with_env_filter(filter) - .with_target(with_target) - .init(); -} - fn get_book_dir(args: &ArgMatches) -> PathBuf { if let Some(p) = args.get_one::("dir") { // Check if path is relative from current dir, or absolute...