tokio-rs / tracing

Application level tracing for Rust.
https://tracing.rs
MIT License
5.53k stars 726 forks source link

Conditionally init of global logger when `tracing-log` feature is enabled #3075

Open sdtnjung opened 2 months ago

sdtnjung commented 2 months ago

Motivation

Solves https://github.com/tokio-rs/tracing/issues/3074

Solution

I want to gather some feedback before putting more work into this PR.

There is a standard trait implementation of SubscriberInitExt. I cannot use a builder pattern for persisting the flag to signal whether a global logger should be initialized or not because I cannot introduce any trait fields.

My workaround is to add a feature gated parameter to init and try_init to then conditionally set the global logger.

I am not sure if you are OK with having feature gated parameters and with such a breaking change.

Regarding the update of the init methods I feel it is a good decision to give (and also enforce) more fine grained control during runtime. I have seen bugs in other repos where people were pulling in tracing-subscriber without being aware of the consequences (of having a global logger set) when tracing-log is enabled by default. Normally you would only have tracing-subscriber down the stream when letting another lib manage your tracing init logic. For such scenarios it makes sense to throw compile time errors in case that specific lib does not support the feature gated with_logger parameter. Imho that is better than unexpected behavior in terms of silently tracing logs.

fn try_init(
    self,
    #[cfg(feature = "tracing-log")] with_logger: bool,
) -> Result<(), TryInitError>

Another alternative could be a new method which then does not prevent existing users from thinking more about the impact of the default behavior