about summary refs log tree commit diff
path: root/compiler
diff options
context:
space:
mode:
authorStypox <stypox@pm.me>2025-06-12 12:09:55 +0200
committerStypox <stypox@pm.me>2025-06-12 12:11:15 +0200
commit781baafbe4501e079489f76fdd6fb439252f467d (patch)
treecadc1f121839e17dc488afe6ab420919c9207a94 /compiler
parentfc96ca8bbad7fb4c7546fb98807e826723fc6c1d (diff)
downloadrust-781baafbe4501e079489f76fdd6fb439252f467d.tar.gz
rust-781baafbe4501e079489f76fdd6fb439252f467d.zip
Add documentation for init_logger_with_additional_layer
Diffstat (limited to 'compiler')
-rw-r--r--compiler/rustc_driver_impl/src/lib.rs7
-rw-r--r--compiler/rustc_log/src/lib.rs11
2 files changed, 14 insertions, 4 deletions
diff --git a/compiler/rustc_driver_impl/src/lib.rs b/compiler/rustc_driver_impl/src/lib.rs
index 038b93dda08..0cd9e36a927 100644
--- a/compiler/rustc_driver_impl/src/lib.rs
+++ b/compiler/rustc_driver_impl/src/lib.rs
@@ -1500,13 +1500,18 @@ pub fn init_rustc_env_logger(early_dcx: &EarlyDiagCtxt) {
 
 /// This allows tools to enable rust logging without having to magically match rustc's
 /// tracing crate version. In contrast to `init_rustc_env_logger` it allows you to choose
-/// the values directly rather than having to set an environment variable.
+/// the logger config directly rather than having to set an environment variable.
 pub fn init_logger(early_dcx: &EarlyDiagCtxt, cfg: rustc_log::LoggerConfig) {
     if let Err(error) = rustc_log::init_logger(cfg) {
         early_dcx.early_fatal(error.to_string());
     }
 }
 
+/// This allows tools to enable rust logging without having to magically match rustc's
+/// tracing crate version. In contrast to `init_rustc_env_logger`, it allows you to
+/// choose the logger config directly rather than having to set an environment variable.
+/// Moreover, in contrast to `init_logger`, it allows you to add a custom tracing layer
+/// via `build_subscriber`, for example `|| Registry::default().with(custom_layer)`.
 pub fn init_logger_with_additional_layer<F, T>(
     early_dcx: &EarlyDiagCtxt,
     cfg: rustc_log::LoggerConfig,
diff --git a/compiler/rustc_log/src/lib.rs b/compiler/rustc_log/src/lib.rs
index 8d959002b56..df648bbd489 100644
--- a/compiler/rustc_log/src/lib.rs
+++ b/compiler/rustc_log/src/lib.rs
@@ -43,8 +43,7 @@ use tracing_subscriber::filter::{Directive, EnvFilter, LevelFilter};
 use tracing_subscriber::fmt::FmtContext;
 use tracing_subscriber::fmt::format::{self, FormatEvent, FormatFields};
 use tracing_subscriber::layer::SubscriberExt;
-// Re-export tracing_subscriber items so rustc_driver_impl doesn't need to depend on it.
-pub use tracing_subscriber::{Layer, Registry};
+use tracing_subscriber::{Layer, Registry};
 
 /// The values of all the environment variables that matter for configuring a logger.
 /// Errors are explicitly preserved so that we can share error handling.
@@ -77,6 +76,11 @@ pub fn init_logger(cfg: LoggerConfig) -> Result<(), Error> {
     init_logger_with_additional_layer(cfg, || Registry::default())
 }
 
+/// Trait alias for the complex return type of `build_subscriber` in
+/// [init_logger_with_additional_layer]. A [Registry] with any composition of [tracing::Subscriber]s
+/// (e.g. `Registry::default().with(custom_layer)`) should be compatible with this type.
+/// Having an alias is also useful so rustc_driver_impl does not need to explicitly depend on
+/// `tracing_subscriber`.
 pub trait BuildSubscriberRet:
     tracing::Subscriber + for<'span> tracing_subscriber::registry::LookupSpan<'span> + Send + Sync
 {
@@ -89,7 +93,8 @@ impl<
 }
 
 /// Initialize the logger with the given values for the filter, coloring, and other options env variables.
-/// Additionally add a custom layer to collect logging and tracing events.
+/// Additionally add a custom layer to collect logging and tracing events via `build_subscriber`,
+/// for example: `|| Registry::default().with(custom_layer)`.
 pub fn init_logger_with_additional_layer<F, T>(
     cfg: LoggerConfig,
     build_subscriber: F,