Skip to content

Can't seem to intercept tracing - ends up in global default instead of overridden subscriber #1187

@vlovich

Description

@vlovich

xitca is an internal implementation detail within my library and I want to redirect its logs to my own tracing subscriber. So in my app I have something like:

#[tokio::main]
async fn main() {
   tracing_subscriber::register().with(tracing_subscriber::fmt::layer()).init();
   let lib_handle = my_lib::init().await.unwrap();
   tokio::signal::ctrl_c().await().unwrap();
}

In my my_lib::init() fn I have something that simplifies to:

async fn init() -> Result<Handle> {
    let dispatcher = Dispatch::new(tracing_subscriber::Register::default().with(tracing_subscriber::filter::LevelFilter::OFF));
    std::thread::spawn(move || {
        tracing::dispatcher::with_default(&dispatcher, move || {
            let rt= tokio::runtime::Builder::new()
                .on_thread_start(move || /*TRACE_TLS_GUARD = tracing::dispatcher::set_default(&dispatcher)*/)
                .on_thread_stop(move ||  */TRACE_TLS_GUARD.reset() */)
           }),enable_all().build();
           let run_app_server = async move {
               tokio::App::new()./*...*/.listen(...).unwrap().run().with_subscriber(dispatcher.clone()).await.unwrap();
           };
           rt.block_on(run_app_server).unwrap();
   });
}

However when I run my executable, I get messages like:

2025-01-29T00:36:34.536074Z  INFO xitca_server::net: Started Tcp listening on: Some(127.0.0.1:8000)
API ready on http://127.0.0.1:8000
2025-01-29T00:36:34.537768Z  INFO xitca_server::worker: Started xitca-server-worker-0
2025-01-29T00:36:34.538002Z  INFO xitca_server::worker: Started xitca-server-worker-1

Obviously my goal isn't to actually turn off the messages. I want them to be silently redirected to my logging endpoint instead rather than bubbling all the way up. I'm just using the "off" as a simple way to verify my subscriber is the one receiving the messages instead of the global default. Any tips on what I'm doing wrong would be appreciated.

Apologies if the example code contains typos - I was trying to reduce it to the smallest possible example without having to build an entirely new working project.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions