-
Notifications
You must be signed in to change notification settings - Fork 823
Description
Bug Report
Version
└── tracing v0.1.40
├── tracing-attributes v0.1.27 (proc-macro)
└── tracing-core v0.1.32
Platform
Fedora 39
uname -a:
Linux scyllaptop 6.7.9-200.fc39.x86_64 #1 SMP PREEMPT_DYNAMIC Wed Mar 6 19:35:04 UTC 2024 x86_64 GNU/Linux
Description
Let me start by saying that I'm not 100% it's a bug - it may be be lack of knowledge in regards to usage of tracing
.
I'm one of maintainters of a library ( https://github.com/scylladb/scylla-rust-driver ) that uses tokio and tracing.
We call .with_current_subscriber()
on each future passed to tokio::spawn
which according to docs ( https://docs.rs/tracing/latest/tracing/instrument/trait.WithSubscriber.html#method.with_current_subscriber )
seems like what we should do as a library.
This works as intended when using tracing subscriber. Example:
Consider a crate with the following Cargo.toml
(it will be used for another example later, thus unused dependencies):
[package]
name = "reproducer"
version = "1.0.0"
edition = "2021"
[dependencies]
env_logger = "0.10"
log = "0.4"
tracing = { version = "0.1.36", features = [ "log" ] }
tracing-subscriber = "0.3"
tokio = { version = "1.27", features = [ "full" ] }
and the following main.rs
:
use tracing::instrument::WithSubscriber;
#[tokio::main]
async fn main() {
let subscriber = tracing_subscriber::FmtSubscriber::new();
tracing::subscriber::set_global_default(subscriber).unwrap();
log::info!("info log");
tracing::info!("info tracing");
tokio::spawn(
async move {
log::info!("spawned info log");
tracing::info!("spawned info tracing")
}
.with_current_subscriber(),
)
.await
.unwrap();
log::info!("another info log");
tracing::info!("another info tracing");
}
After cargo run
traces are printed and logs are not, as expected:
cargo run
Compiling reproducer v1.0.0 (/tmp/tracing_issue)
Finished dev [unoptimized + debuginfo] target(s) in 0.56s
Running `target/debug/reproducer`
2024-03-18T15:48:35.651650Z INFO reproducer: info tracing
2024-03-18T15:48:35.651700Z INFO reproducer: spawned info tracing
2024-03-18T15:48:35.651727Z INFO reproducer: another info tracing
Now let's switch tracing subscriber for env_logger. As far as I understand from tracing
's docs env_logger should print my traces because I enabled log
feature on tracing
crate.
New main.rs:
use tracing::instrument::WithSubscriber;
#[tokio::main]
async fn main() {
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
log::info!("info log");
tracing::info!("info tracing");
tokio::spawn(
async move {
log::info!("spawned info log");
tracing::info!("spawned info tracing")
}
.with_current_subscriber(),
)
.await
.unwrap();
log::info!("another info log");
tracing::info!("another info tracing");
}
Output:
cargo run
Compiling reproducer v1.0.0 (/tmp/tracing_issue)
Finished dev [unoptimized + debuginfo] target(s) in 0.78s
Running `target/debug/reproducer`
[2024-03-18T15:53:43Z INFO reproducer] info log
[2024-03-18T15:53:43Z INFO reproducer] info tracing
[2024-03-18T15:53:43Z INFO reproducer] spawned info log
[2024-03-18T15:53:43Z INFO reproducer] another info log
As you can see, no traces are printed after the tokio::spawn
call - interestingly it affects even the tracing call in the same function.
It's not the problem with log
calls - issue persists even if I remove them.
This results in our library not being usable with log
loggers like env_logger
even if we enable log
feature :(