-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Allow initializing logger with additional tracing Layer #140969
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,6 +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; | ||
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. | ||
|
@@ -72,6 +73,36 @@ impl LoggerConfig { | |
|
||
/// Initialize the logger with the given values for the filter, coloring, and other options env variables. | ||
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: | ||
Stypox marked this conversation as resolved.
Show resolved
Hide resolved
|
||
tracing::Subscriber + for<'span> tracing_subscriber::registry::LookupSpan<'span> + Send + Sync | ||
{ | ||
} | ||
|
||
impl< | ||
T: tracing::Subscriber + for<'span> tracing_subscriber::registry::LookupSpan<'span> + Send + Sync, | ||
> BuildSubscriberRet for T | ||
{ | ||
} | ||
|
||
/// 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 via `build_subscriber`, | ||
/// for example: `|| Registry::default().with(custom_layer)`. | ||
pub fn init_logger_with_additional_layer<F, T>( | ||
cfg: LoggerConfig, | ||
build_subscriber: F, | ||
) -> Result<(), Error> | ||
where | ||
F: FnOnce() -> T, | ||
T: BuildSubscriberRet, | ||
{ | ||
let filter = match cfg.filter { | ||
Ok(env) => EnvFilter::new(env), | ||
_ => EnvFilter::default().add_directive(Directive::from(LevelFilter::WARN)), | ||
|
@@ -124,7 +155,7 @@ pub fn init_logger(cfg: LoggerConfig) -> Result<(), Error> { | |
Err(_) => {} // no wraptree | ||
} | ||
|
||
let subscriber = tracing_subscriber::Registry::default().with(filter).with(layer); | ||
let subscriber = build_subscriber().with(layer.with_filter(filter)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So making this This is the one part of the PR I am not sure about, since I don't know much about tracing. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, because it will apply the filter to every layer, including any custom layer we might add There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So despite the fact that the filter is added after the custom layers, it takes effect before them, preventing events from reaching them...? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, see the last paragraph here: https://docs.rs/tracing-subscriber/0.3.16/tracing_subscriber/layer/index.html#global-filtering There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wow, that's not what I expected.^^ |
||
match cfg.backtrace { | ||
Ok(backtrace_target) => { | ||
let fmt_layer = tracing_subscriber::fmt::layer() | ||
|
Uh oh!
There was an error while loading. Please reload this page.