Skip to content

Using tracing_appender nothing is printed in terminal, why? #2263

Answered by hawkw
frederikhors asked this question in Q&A
Discussion options

You must be logged in to vote

Your init function is dropping the WorkerGuard returned by calling tracing_appender::non_blocking when it exits. Dropping this guard shuts down the non-blocking appender's worker thread, so the worker thread shuts down when your init function returns.

You can fix this by changing your code to return the guard from init and hold it in your main function, like this:

use tracing::info;

fn main() {
  let _guard = init();

  info!("test me");

  println!("i should have info! message before this")
}

pub fn init() -> impl Drop {
  let (non_blocking, guard) = tracing_appender::non_blocking(std::io::stdout());

  tracing_subscriber::fmt()
    .with_writer(non_blocking) 
    .init();

  guard
}

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by frederikhors
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #2262 on August 03, 2022 19:02.