Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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 }
Expand Down
1 change: 1 addition & 0 deletions crates/mdbook-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
43 changes: 43 additions & 0 deletions crates/mdbook-core/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
35 changes: 1 addition & 34 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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::<PathBuf>("dir") {
// Check if path is relative from current dir, or absolute...
Expand Down