Skip to content

Commit 781baaf

Browse files
committed
Add documentation for init_logger_with_additional_layer
1 parent fc96ca8 commit 781baaf

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

compiler/rustc_driver_impl/src/lib.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1500,13 +1500,18 @@ pub fn init_rustc_env_logger(early_dcx: &EarlyDiagCtxt) {
15001500

15011501
/// This allows tools to enable rust logging without having to magically match rustc's
15021502
/// tracing crate version. In contrast to `init_rustc_env_logger` it allows you to choose
1503-
/// the values directly rather than having to set an environment variable.
1503+
/// the logger config directly rather than having to set an environment variable.
15041504
pub fn init_logger(early_dcx: &EarlyDiagCtxt, cfg: rustc_log::LoggerConfig) {
15051505
if let Err(error) = rustc_log::init_logger(cfg) {
15061506
early_dcx.early_fatal(error.to_string());
15071507
}
15081508
}
15091509

1510+
/// This allows tools to enable rust logging without having to magically match rustc's
1511+
/// tracing crate version. In contrast to `init_rustc_env_logger`, it allows you to
1512+
/// choose the logger config directly rather than having to set an environment variable.
1513+
/// Moreover, in contrast to `init_logger`, it allows you to add a custom tracing layer
1514+
/// via `build_subscriber`, for example `|| Registry::default().with(custom_layer)`.
15101515
pub fn init_logger_with_additional_layer<F, T>(
15111516
early_dcx: &EarlyDiagCtxt,
15121517
cfg: rustc_log::LoggerConfig,

compiler/rustc_log/src/lib.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ use tracing_subscriber::filter::{Directive, EnvFilter, LevelFilter};
4343
use tracing_subscriber::fmt::FmtContext;
4444
use tracing_subscriber::fmt::format::{self, FormatEvent, FormatFields};
4545
use tracing_subscriber::layer::SubscriberExt;
46-
// Re-export tracing_subscriber items so rustc_driver_impl doesn't need to depend on it.
47-
pub use tracing_subscriber::{Layer, Registry};
46+
use tracing_subscriber::{Layer, Registry};
4847

4948
/// The values of all the environment variables that matter for configuring a logger.
5049
/// Errors are explicitly preserved so that we can share error handling.
@@ -77,6 +76,11 @@ pub fn init_logger(cfg: LoggerConfig) -> Result<(), Error> {
7776
init_logger_with_additional_layer(cfg, || Registry::default())
7877
}
7978

79+
/// Trait alias for the complex return type of `build_subscriber` in
80+
/// [init_logger_with_additional_layer]. A [Registry] with any composition of [tracing::Subscriber]s
81+
/// (e.g. `Registry::default().with(custom_layer)`) should be compatible with this type.
82+
/// Having an alias is also useful so rustc_driver_impl does not need to explicitly depend on
83+
/// `tracing_subscriber`.
8084
pub trait BuildSubscriberRet:
8185
tracing::Subscriber + for<'span> tracing_subscriber::registry::LookupSpan<'span> + Send + Sync
8286
{
@@ -89,7 +93,8 @@ impl<
8993
}
9094

9195
/// Initialize the logger with the given values for the filter, coloring, and other options env variables.
92-
/// Additionally add a custom layer to collect logging and tracing events.
96+
/// Additionally add a custom layer to collect logging and tracing events via `build_subscriber`,
97+
/// for example: `|| Registry::default().with(custom_layer)`.
9398
pub fn init_logger_with_additional_layer<F, T>(
9499
cfg: LoggerConfig,
95100
build_subscriber: F,

0 commit comments

Comments
 (0)