Using tracing_appender nothing is printed in terminal, why? #2263
Answered
by
hawkw
frederikhors
asked this question in
Q&A
-
Bug ReportThis code doesn't print anything on my terminal: use tracing::info;
fn main() {
init();
info!("test me");
println!("i should have info! message before this")
}
pub fn init() {
let (non_blocking, _guard) = tracing_appender::non_blocking(std::io::stdout());
tracing_subscriber::fmt()
// .with_writer(non_blocking) // with this info! is not printed
.init();
} Version
PlatformWindows 10 64 |
Beta Was this translation helpful? Give feedback.
Answered by
hawkw
Aug 3, 2022
Replies: 1 comment
-
Your You can fix this by changing your code to return the guard from 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
} |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
frederikhors
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Your
init
function is dropping theWorkerGuard
returned by callingtracing_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 yourinit
function returns.You can fix this by changing your code to return the guard from
init
and hold it in yourmain
function, like this: